myscp=$(sshpass -p testserver scp -rP 8888 root@localhost:/test/server/ ~/local/)How can I add conditional statement to this line? If its pipe broken scp will stop/break otherwise if it's open for connection it will continue
if "$myscp" == 0; then echo "SUCCESS)."
else echo "**FAILED)." echo "$myscp" exit echo "Done........ 100.0%"This example does not work
2 Answers
First example saves the return code from sshpass:
#!/bin/bash
sshpass -p password \
scp -rq -P 22 user@localhost:~/src/path ~/dest/path
c=$?
if ((!$c)); then echo "Success!"
else echo "Failed: exit code ($c)."
fiOr you could use your command directly in an if-then statement:
#!/bin/bash
if sshpass -p password \
scp -rq -P 22 user@localhost:~/src/path ~/dest/path; then echo "Success!"
else echo "Failed!"
fiI strongly recommend that you do not use passwords but instead use key authentication.
The $? variable is set to the return code of the last executed command
sshpass -p testserver scp -rP 8888 root@localhost:/test/server/ ~/local/
if [ $? == 0 ]; then echo "SUCCESS)."
else echo "**FAILED)." echo "Exit code: $?" exit echo "Done........ 100.0%"
fi