I have a shell script that I'm working on with this line of code that does a loop through local files (.gz) and does an scp. I want to test for a failed scp
if possible. I am doing a loop so I can echo each file name to a log so I can keep track of it.
Can someone show me how to check for failed scp? or better yet, a good code example to do this? Thanks for your help.
for gzfile in $LOCALDMPDIR/*.gz
do
/usr/bin/scp -P 2222 -i $KEYFILE $gzfile foobar@$1:$TGTDIR
echo "$gzfile is done. " 2>&1
done
Use the command which scp . It lets you know whether the command is available and it's path as well. If scp is not available, nothing is returned.
I can run scp -p one. txt user@server:/one. txt and the output will show one.
$? is a special variable in shell that reads the exit status of the last command executed. After a function returns, $? gives the exit status of the last command executed in the function.
Use $? to access the return value of the last command. Check the man page for scp to verify, but I think a return value of zero means success. A non-zero value means some kind of failure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With