LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to exit shell script if remote login unsuccessful? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-exit-shell-script-if-remote-login-unsuccessful-4175508112/)

rsh015 06-15-2014 02:41 PM

How to exit shell script if remote login unsuccessful?
 
Code:

#!/bin/bash
for servers in `cat ~/servers`
do
rosh -l root -n $servers 'if [ -d /opt/ca ]
then
echo $HOSTNAME
else
exit 1
fi'
done

I am executing above script in opsware global shell. I have few servers in the for loop that is powered off, so whenever I execute my script, it works fine if all the servers are on, but when it tries to execute the script in the servers that are powered off, my script just hangs there. Please provide me with some hints. I am new to shell scripting. Thank you very much

coralfang 06-15-2014 04:30 PM

You could use $?

Code:

if [[ $? != 0 ]] ; then
    echo "command failed!"
fi

Each time you run a command, you can check $? for an exit status code.


All times are GMT -5. The time now is 06:52 AM.