Reputation: 2635
I have a script that open up cores on machine It works fine, but if I enhance it, like if there are not cores on the machine, I would like it to tell me, not just kick out an error command. I would like to replace the error with something So I am trying to ge the exit status of the last command and base the if statement off of that.
#!/bin/bash
rc=$?
for i in server5432foo server5438foo server5344foo server5430foo;
do
echo $i ;
ssh -q -T $i " ls -ltr /local/data/log/core*; echo $? ";
echo $rc
if [[ $rc == 0 ]] ; then
ssh -q -T $i " chmod ugo+rw /local/data/log/core* ";
else
echo " No core files on $i /local/data/log/ "
fi
done
This is what the script returns where there are no cores in the file specified. Even though the ls is unsuccessful it still returns a 0 and does not run the else command.
ls: cannot access /local/data/log/core*: No such file or directory
0
0
chmod: cannot access `/local/data/log/core*': No such file or directory
I am trying to get the return code on the commnand sent to that box vias ssh. In the script it seems that the SSH is successful, so it returns a zero. However when I run the command from the shell - I get a return code other than zero if it is unsuccessful.
casper@server0170foo:~/walt/restart_superprocesss$ ssh -q -T server5432foo "ls -ltr /local/data/log/core00001" ; echo $?
ls: cannot access /local/data/log/core00001: No such file or directory
2
casper@server0170foo:~/walt/restart_superprocesss$ ssh -q -T server5432foo "touch /local/data/log/core00001" ; echo $?
0
casper@server0170foo:~/walt/restart_superprocesss$ ssh -q -T server5432foo "ls -ltr /local/data/log/core00001" ; echo $?
-rw-r--r-- 1 casper casper 0 Oct 13 11:38 /local/data/log/core00001
0
How come the return code is different from when I run a command directly from the command line, which are accurate as opposed to running the command from a script - where I get a 0 everytime
Upvotes: 0
Views: 432
Reputation: 10324
I would try the following:
for i in server5432foo server5438foo server5344foo server5430foo; do
echo $i;
if ssh -q -T $i " ls -ltr /local/data/log/core* > /dev/null; then
ssh -q -T $i "chmod ugo+rw /local/data/log/core*"
else
echo "No core files on $i /local/data/log/"
fi
done
Upvotes: 1
Reputation: 1700
If you want to check the return status of ssh
(which should be return status of the remotely executed command or some higher number indicating ssh
itself failed), I think you have a bug in your script, and you want to remove the echo in the ssh
statement, because it will exit with 0 (and because, with double-quotes, the $?
will be the return value of the command before ssh
, not the return value of the ls
):
#!/bin/bash
# This line isn't really helpful
#rc=$?
for i in server5432foo server5438foo server5344foo server5430foo;
do
echo $i ;
ssh -q -T $i "ls -ltr /local/data/log/core*";
# This is the new line to make sure $rc has the ssh exit status
rc=$?
echo $rc
if [[ $rc == 0 ]] ; then
ssh -q -T $i "chmod ugo+rw /local/data/log/core*";
else
echo " No core files on $i /local/data/log/ "
fi
done
From the man page:
EXIT STATUS ssh exits with the exit status of the remote command or with 255 if an error occurred.
Upvotes: 1