Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best way to programmatically check for a failed scp in a shell script

Tags:

bash

shell

unix

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
like image 805
jdamae Avatar asked Mar 11 '11 00:03

jdamae


People also ask

How do I know if SCP is working?

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.

How do I know if SCP is successful Linux?

I can run scp -p one. txt user@server:/one. txt and the output will show one.

What is $? In shell script?

$? 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.


1 Answers

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.

like image 163
Brent Worden Avatar answered Sep 24 '22 23:09

Brent Worden