Automating FTP from Unix Shell Script

Recently I faced an issue about FTP. I was supposed to ask the name and location of the file as one of the input to the user and get the file at current host where I am running the script. The situation was bit complex. I mean, the inputs from the user were from a GUI tool and that was getting passed to one  of the shell script. One of the challenge that I faced was FTPing the file from some other server to the current server. Because normal FTP command ask for user ID and password and is interactive.

But with little investigation I was able to get the correct command options for FTP that can be used inside a shell script and its a totally non-interactive version. Here is the FTP code I used.

echo "echo \"open statj11" >> $HOME/
echo "quote USER ora1818" >> $HOME/
echo "quote PASS ORA1818" >> $HOME/
echo "bin" >> $HOME/
echo "close" >> $HOME/
echo "quite\" | ftp -n" >> $HOME/

sh $HOME/

Basically I am connecting to statj11 server as ora1818 user. The password for this user is ORA1818. EMSUI_SOURCE_LOCATION is the variable which has name and location of the file to be FTPed. I am taking the name and location (complete path) of the file to be FTPed from the user. (EMSUI_SOURCE_LOCATION=$1)

`basename $EMSUI_SOURCE_LOCATION` will give you the name of the file to be FTPed, which I am using as the name of my FTPed file.

All the above commands are echoed to a file called $HOME/ and then I am executing this script which will FTP the required file.

Hope this helps !!

One thought on “Automating FTP from Unix Shell Script

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s