M BUZZ CRAZE NEWS
// general

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)."
fi

Or 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!"
fi

I 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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy