[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 636
  • Last Modified:

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.
0
chanikya
Asked:
chanikya
  • 6
  • 3
1 Solution
 
BlueYonderCommented:
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
0
 
woolmilkporcCommented:
Hi,

To avoid being prompted for the password you must use "ftp -n"  and

with some FTP clients you must put the command "user" and the actual userid and password on two separate lines:

user
xxxx pass

or use "quote":

quote user xxx
quote pass pass

Next, FTP does not understand "if" or "then" etc. In fact there is no conditional processing at all in the FTP client.

As a workaround, you could issue the rename command regardless of whether the remote file already exists or not.
A possible error message is harmless and can be ignored.

#!/bin/sh
fname="test_file.csv"
yesterday=`TZ=GMT+24 date +%Y%m%d%H%M`
ftp -n  <<ENDSCRIPT 2>/dev/null
open "xxxx"
user 
xxxx pass
rename "${fname}" "test_file_${yesterday}.csv"
put "${fname}" 
quit
ENDSCRIPT

Open in new window

0
 
chanikyaAuthor Commented:
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Thanks,
Chanikya.
0
 
chanikyaAuthor Commented:
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..
0
 
chanikyaAuthor Commented:
I am sorry , i have not chose the grade
0
 
chanikyaAuthor Commented:
It works
0
 
woolmilkporcCommented:
Yes. But sorry, I think we must use ">/dev/null", not "2>/dev/null". FTP does not use stderr.
0
 
chanikyaAuthor Commented:
It is the correct answer.. and will solve my purpose temporarily.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now