0

I am trying to use curl to get a list of files on my ftp server and store that data in a variable to be used with a for loop to download and then delete each file from the ftp server.

the issue is am having is that whist i can get the list of variables out with a simple echo command i cannot get the variables to pass to the curl script properly.

EG.

$ while read i; do echo "$i"; done <ftpinputfiles
datapush_tracked_object_2019-04-28T04-17-30Z.json
datapush_tracked_object_2019-04-28T04-18-00Z.json
datapush_tracked_object_2019-04-28T04-18-30Z.json
datapush_tracked_object_2019-04-28T04-19-00Z.json
datapush_tracked_object_2019-04-28T04-19-30Z.json
datapush_tracked_object_2019-04-28T04-20-00Z.json
datapush_tracked_object_2019-04-28T04-20-30Z.json
datapush_tracked_object_2019-04-28T04-21-00Z.json
datapush_tracked_object_2019-04-28T04-21-30Z.json
datapush_tracked_object_2019-04-28T04-22-00Z.json
datapush_tracked_object_2019-04-28T04-22-30Z.json
datapush_tracked_object_2019-04-28T04-23-00Z.json

How ever if i run this command to see if the variables are passing to the curl command it does not run properly

$ while read i; do echo "ftp://123.123.123.123/Test/$i --user USER:PASS -o $i"; done <ftpinputfiles
 --user admin:PASS -o datapush_tracked_object_2019-04-28T04-17- 
 30Z.json
 --user admin:PASS -o datapush_tracked_object_2019-04-28T04-18- 
 00Z.json
 --user admin:PASS -o datapush_tracked_object_2019-04-28T04-18- 
 30Z.json
 --user admin:PASS -o datapush_tracked_object_2019-04-28T04-19- 
 00Z.json
 --user admin:PASS -o datapush_tracked_object_2019-04-28T04-19- 
 30Z.json
 --user admin:PASS -o datapush_tracked_object_2019-04-28T04-20- 
 00Z.json
 --user admin:PASS -o datapush_tracked_object_2019-04-28T04-20- 
 30Z.json
 --user admin:PASS -o datapush_tracked_object_2019-04-28T04-21- 
 00Z.json 
 --user admin:PASS -o datapush_tracked_object_2019-04-28T04-21- 
 30Z.json
 --user admin:PASS -o datapush_tracked_object_2019-04-28T04-22- 
 00Z.json
 --user admin:PASS -o datapush_tracked_object_2019-04-28T04-22- 
 30Z.json
 --user admin:PASS -o datapush_tracked_object_2019-04-28T04-23- 
 00Z.json
oguz ismail
  • 1
  • 16
  • 47
  • 69
  • Which error do you get? – Cyrus Apr 28 '19 at 05:10
  • that is not formatting the output correctly, i would expect the output to look like ftp://123.123.123.123/Test/datapush_tracked_object_2019-04-28T04-23- 00Z.json --user USER:PASS -o datapush_tracked_object_2019-04-28T04-23- 00Z.json but i am only getting the output i have listed at the bottom of the post – B.Seabrook Apr 28 '19 at 05:12
  • This works fine: `echo "foo" | while read i; do echo "ftp://123.123.123.123/Test/$i --user USER:PASS -o $i"; done` – Cyrus Apr 28 '19 at 05:14
  • Why is there a space before `00Z.json`? – Cyrus Apr 28 '19 at 05:16
  • the space before the 00z.json happened when i copied the output to Stack overflow, its not in the terminal window. – B.Seabrook Apr 28 '19 at 05:19
  • 1
    Then fix it on Stack overflow so we can see what the real problem might be without any red herrings like that. Showing us something that's not exactly what you're looking at is like showing your mechanic your motorcycle and asking him to diagnose the problem with your car. – Ed Morton Apr 28 '19 at 05:41
  • 5
    My guess is your lines in `ftpinputfiles` end in `\r\n`. See https://stackoverflow.com/q/45772525/1745001. – Ed Morton Apr 28 '19 at 05:44
  • 1
    You can get the `read` command to remove a trailing carriage return by using `while IFS=$' \t\n\r' read i;...` Note that this changes `IFS` only for the `read` command itself, so you don't have to set it back to normal afterward. – Gordon Davisson Apr 28 '19 at 05:59
  • Thanks Ed Morton, That solved it, and i can fix the issue with the format above because it is in a comment and i don't think i can edit that comment. – B.Seabrook Apr 28 '19 at 06:11

0 Answers0