Unable to Download File from FTP

Hi,

I am trying to download files from FTP using below FTP code.

 x=$(TZ=CST date +%Y%m%d)
host='xxx.xx.xxx.xxx'
user='userX'
pass='Password'
echo "Connecting to FTP Host -- $host....."
echo $x
a=$(ftp -v -n -i << !
open $host
user  "$user" $pass
cd /rose/yellow/
mget Daily_rose_$x_report.csv
!)
 

Ideally file Daily_rose_20170802_report.csv should get downloaded from FTP to my local drive.

I am getting the below error

 The system cannot find the file specified.
Cannot find list of remote files.
 

Request in support to debug the above code.

Regards,
Ram

Mayhap stating the obvious: Does /rose/yellow/Daily_rose_20170802_report.csv exist on the target host, and does the remote user have access permission?

First, don't put username and password in a script. Use instead a file called .netrc

$ cat ~/.netrc
machine xxx.xx.xxx.xxx login userX password xxxxx

Second, the mget command is for multiple files. Try using get instead.

Are you sure the file exists? What happens when you use the ls command to ftp instead?

Also, consider installing and using lftp instead. Not only does it grok multiple protocols including FTP, SFTP and FTPS; it comes with a shell wrapper lftpget for fetching single or multiple files.

Andrew

Hi,

Yes the file does exist, the problem is when file name ends with date

Daily_rose_20170802.csv 

my code is working perfectly, but when file name is

Daily_rose_20170802_report.csv 

after date any other string in file name, then the code is giving error. Moreover I am using CYGWIN to perform the task.

Regards,
Ram

Enclose the x variable expansion in braces, like ..._${x}_... .

2 Likes

To back up what my learned friend RudiC says, the reason is that the underscore is not seen as delimiting the end of the variable. It is an allowable character in the variable name, so you will end up evaluating $x_report which is probably not set, so you will always try to get Daily_rose_.csv If the file had used hyphens instead of underscores then you would have got away with it, but only by luck.

Some shells also have limitations on arguments, e.g. $1 . As a minor war-story, I had to debug a script once which accepted ten arguments and referred to $10 It took me ages to work out was that $10 was actually the value of $1 followed by a zero and this was required by some odd processing in the script. When it needed to use the value of $10 , the author had done a shift and referred to $9 instead confusing everything else as the script looped.

The escape from this madness was to use ${10} and remove the shift, so it is good practice to wrap variable names in {} anyway.

Kind regards,
Robin

1 Like

Hi Friends,

The solution provided had worked perfectly, and thanks for your detailed input.

Regards,
Ram