Reputation: 41
I need to make a Unix script, where I need both FTP and SFTP functionality. Let me describe in details:
I need to import few files from remote server, now these remote server either support ftp or SFTP not both. So I need a script where my script will try to do ftp first and if it gets succeeded then come out of script and if not then try to do SFTP on that server.
I tried the below script:
log=/opt/app/vertica/cdr/tmp/hpcom/sftp.txt
LOGINID=abc
HOST=sftp2.xyz.com
echo "starting ftp & sftp...." > $log
ftp -i -n -v $HOST <<-EOFtp >$log
user $LOGINID $PASSWD
bye
EOFtp
rc=$?
if [[ $rc = 0 ]]; then
echo "Successful ftp...$rc" `date "+%Y-%m-%d-%H.%M.%S"` >> $log
else
sftp $LOGINID@$HOST<<EOF > $log
#cd $DIR
#get $FILE
bye
EOF
rc1=$?
if [[ $rc1 != 0 ]]; then
echo "Error occured...$rc" `date "+%Y-%m-%d-%H.%M.%S"` >> $log
else
echo "Successful sftp...$rc" `date "+%Y-%m-%d-%H.%M.%S"` >> $log
fi
fi
In above code my script is not working as it should... below is the output of log file
"Not connected.
Successful ftp...0 2014-01-30-10.03.06"
It clearly shows that FTP is not happening on this host so ideally it should try doing SFTP but its not coming to SFTP syntax rather its coming out of script...
I hope I included everything, please let me know if you need any further information to assist with this.
Upvotes: 2
Views: 533
Reputation: 207425
You need to use "-eq" instead of "=" when comparing numbers:
if [[ $rc -eq 0 ]]; then...
if [[ $rc1 -ne 0 ]]; then...
Upvotes: 1