How can I check condition statement for ssh scp over the server
By Sarah Rodriguez •
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 More in general
"Zoraya ter Beek, age 29, just died by assisted suicide in the Netherlands. She was physically healthy, but psychologically depressed. It's an abomination that an entire society would actively facilitate, even encourage, someone ending their own life because they had no hope. Th…"