user3573036
user3573036

Reputation: 3

How to use multiple if statements

I have a created a bash script which touches a file in specific mounts to monitor for directory locks or storage issues. I've done this using multiple if statements, but if I use the below syntax using exit at the end of the if then this exits the full script and not continue with checking the rest of the server hosts. Can someone tell me if there's either a better way of doing this or if I can replace the exit so that the script continues with the rest of the if statements?

ssh $SERVER1 touch /apps/mount/im.alive.txt
if [ $? -ne 0 ]; then
echo "$SERVER1 is in accessible. Please escalate"
else
exit
fi

ssh $SERVER2 touch /apps/mount/im.alive.txt
if [ $? -ne 0 ]; then
echo "$SERVER2 is in accessible. Please escalate"
else
exit
fi

Upvotes: 0

Views: 159

Answers (4)

clt60
clt60

Reputation: 63974

And finally, (after all good recommendations) it is a good practice using functions for some actions, like error messages and such. Therefore, the

#put this at the top of your script
eecho() {
    echo "Error: $@" >&2
    return 1
}

will function as an echo, but always write the error message to STDERR, and returns problem (non zero status) so you can do the next:

 [[ some_condition ]] || eecho "some error message" || exit 1

e.g. chain it with exit. (see konsolebox's recommendation)

Upvotes: 0

Kenster
Kenster

Reputation: 25458

You don't have to run "ssh" and then explicitly test its exit code. The "if" command will do that for you. This is how I would write that:

if ssh $SERVER1 touch /apps/mount/im.alive.txt
then
    true  # do-nothing command
else
    echo "$SERVER1 is in accessible. Please escalate"
fi

if ssh $SERVER2 touch /apps/mount/im.alive.txt
then
    true  # do-nothing command
else
    echo "$SERVER2 is in accessible. Please escalate"
fi

However, since you're performing the same set of operations on more than one SERVER, you could use a loop:

for server in $SERVER1 $SERVER2
do
    if ssh $server touch /apps/mount/im.alive.txt
    then
        true  # do-nothing command
    else
        echo "$server is in accessible. Please escalate"
    fi
done

Upvotes: 0

konsolebox
konsolebox

Reputation: 75608

Or just simplify it like this:

ssh "$SERVER1" touch /apps/mount/im.alive.txt || \
    echo "$SERVER1 is in accessible. Please escalate"

ssh "$SERVER2" touch /apps/mount/im.alive.txt || \
    echo "$SERVER2 is in accessible. Please escalate"

Or

for S in "$SERVER1" "$SERVER2"; do
    ssh "$S" touch /apps/mount/im.alive.txt || \
        echo "$S is in accessible. Please escalate."
done

You can also turn it into a script:

#!/bin/sh
for S; do
    ssh "$S" touch /apps/mount/im.alive.txt || \
        echo "$S is in accessible. Please escalate."
done

Usage:

sh script.sh "$SERVER1" "$SERVER2"

Upvotes: 0

kamituel
kamituel

Reputation: 35970

To elaborate on the comment by @Mighty Portk: the else part of the if statement is not mandatory, so you can just get away without it, and without the exit:

ssh $SERVER1 touch /apps/mount/im.alive.txt
if [ $? -ne 0 ]; then
  echo "$SERVER1 is in accessible. Please escalate"
fi

ssh $SERVER2 touch /apps/mount/im.alive.txt
if [ $? -ne 0 ]; then
  echo "$SERVER2 is in accessible. Please escalate"
fi

Upvotes: 1

Related Questions