Link to home
Start Free TrialLog in
Avatar of chanikya
chanikya

asked on

ftp'ing a file using shell script

Hi Experts,

I have the below requirement . Please provide valuable suggestions.

As i am newbie to shell script.. probably it may be the simpler for you

1) I have to ftp a file to remote server
2) In remote location if the file already exist then i have to rename the file.
For ex:-  assume that test_file.txt already exist then test_file_20120821.txt( as yesterdays file)
3) then i have to ftp the current file to remote location..

I have just tried the below code

But  It is prompting for password and after that it is saying invalid command

#!/bin/sh

fname="test_file.csv"
yesterday=`TZ=GMT+24 date +%Y%m%d%H%M`
ftp  <<ENDSCRIPT
open "xxxx"
user xxxx pass

if test -f "${fname}"
then
rename "${fname}" "test_file_${yesterday}.csv"
fi
quit
ENDSCRIPT

Open in new window


Thanks
Chanikya.
Avatar of BlueYonder
BlueYonder

Here is a working example

#!/bin/sh
HOST='ftp.users.qwest.net'
USER='yourid'
PASSWD='yourpw'
FILE='file.txt'

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put $FILE
quit
END_SCRIPT
exit 0
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chanikya

ASKER

Hi Experts,

Thank you Both.. Now i am able to login with username and password.
After that still i am getting invalid command after that..

cann't  I use IF command in ftp.  How can i verify the file exist or not in the remote location.

Thanks,
Chanikya.
Why didn't you read my comment up to the end?
I am really sorry. You mean even if there is an error it won't stop the further processing of shell script.

Thanks,
Chanikya.
I've requested that this question be closed as follows:

Accepted answer: 0 points for chanikya's comment #a38320114

for the following reason:

oh sorry.. I got it .. you are supressing the error by &nbsp;2&gt;dev/null.. Am i correct..
I am sorry , i have not chose the grade
It works
Yes. But sorry, I think we must use ">/dev/null", not "2>/dev/null". FTP does not use stderr.
It is the correct answer.. and will solve my purpose temporarily.