Soham
September 9, 2013, 1:19pm
1
I am not able to capture error condition in sftp in Korn Shell
#!/bin/ksh
sftp batch@uat >abc 2>&1 << ENDFILE
cd public
put /data/WELCOME_55
ENDFILE
ret_val=$?
if [[ $ret_val -eq 0 ]]
then
print file "copied successfully"
else
print file "NOT copied successfully"
fi
return 0
Now the file /data/WELCOME_55 does not exist. I am seeing the error message in file abc. But ret_val is zero.
Any help?
Thanks - Soham
Scott
September 9, 2013, 1:30pm
2
I couldn't get it to work, either.
Maybe you could check if the file exists after SFTP is done?
if [ -f filename ]; then
...
else
...
fi
Soham
September 9, 2013, 1:35pm
3
Yes that will work if the file does not exist in destination server. If it exists and the sftp has failed, still we will get SUCCESS messaage.
Try running sftp in batch mode...see man pages, should return 1 in this case. Also try running sftp in verbose modes so you see the nature of the failure.
1 Like
Scott
September 9, 2013, 1:45pm
5
Oh yes, I saw that but didn't think to test it. Shows how much I use SFTP
Soham
September 9, 2013, 1:51pm
6
I have tried that also. But no success.
Scott
September 9, 2013, 1:59pm
7
Having read the man page, I'd assume that would work.
Here's what I use!
$ ssh someone@somewhere '[ -f /tmp/temp.txt ] && cat /tmp/temp.txt || exit 1' > temp.txt
$ echo $?
1
$ ssh someone@somewhere 'touch /tmp/temp.txt'
$ ssh someone@somewhere '[ -f /tmp/temp.txt ] && cat /tmp/temp.txt || exit 1' > temp.txt
$ echo $?
0
I am using below code, please check, output also mentioned.
script.sh
sshpass -p password sftp username@server.com << ENDFILE
cd httpdocs
put test.php
ENDFILE
if [[ $ret_val -eq 0 ]]
then
echo "file copied successfully"
else
echo "file NOT copied successfully"
fi
# sh script.sh
Connected to server.com.
sftp> cd httpdocs
sftp> put test.php
Uploading test.php to /var/www/server.com/httpdocs/test.php
file copied successfully
Scott
September 9, 2013, 3:09pm
9
learnbash:
I am using below code, please check, output also mentioned.
script.sh
sshpass -p password sftp username@server.com << ENDFILE
cd httpdocs
put test.php
ENDFILE
if [[ $ret_val -eq 0 ]]
then
echo "file copied successfully"
else
echo "file NOT copied successfully"
fi
# sh script.sh
Connected to server.com.
sftp> cd httpdocs
sftp> put test.php
Uploading test.php to /var/www/server.com/httpdocs/test.php
file copied successfully
The problem is what to do when the file is not copied.
BTW, I don't see where you set ret_val
I am sorry scott, i miss the part.
script.sh
sshpass -p password sftp username@server.com << ENDFILE
cd httpdocs
put test.php
ENDFILE
ret_val=$?
if [[ $ret_val -eq 0 ]]
then
echo "file copied successfully"
else
echo "file NOT copied successfully"
fi