Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a script that adds user on many server. Since I'm adding on many server(s) I am using for loop.

Below are the output when user is successfully added.

0,22529,User added.

If the user is not added, then I get this error

1,23996,CLI failed to connect to database.

OR

1,22560,The current state of the server does not allow this action.

script Begin

read user
echo -e '\E[32;40m'"\033[1mPlease wait .... adding id on all server\033[0m"
PORT=777
USER=User
PASSWORD=****
for server in server1.abc.com server2.abc.com 
do
command // to add user 
done

Script end

I want echo of both user added and user not added based on server list passed for loop.

Eg.

User added in server1.abc.com
user failed to add in server2.abc.com

Any help on this would be highly appreciated.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

If you want to use the standard output of a certain command, you can use command substitution and wrap it inside a $(…).

We can check if the output conforms to a certain regular expression with the =~ operator introduced in Bash 3 and above. If the regular expression matches, the test will return true, so we can execute another command after &&. If it doesn't match, the test will return false, and the commands after || are executed.

Since we want to check whether the output contains the word added, the following is enough:

for server in server1.abc.com server2.abc.com 
do
  [[ "$(command)" =~ added ]] \
    && echo "User ${user} added in ${server}" \
    || echo "User ${user} failed to add in ${server}"
done

In your script you should change USER to user, since $USER is a global environment variable carrying your username. Try running echo $USER from your command line to see what I mean.

share|improve this answer
    
yes, it is generally advised to use lower case variable names, so you don't accidentally overwrite bash variables. –  glenn jackman Jan 20 '13 at 13:48
    
Thanks Slhck and Glenn! –  maneeshshetty Jan 20 '13 at 15:10
    
You're welcome, @maneeshshetty. Did you know the Super User way of saying "thank you" is voting up answers you found useful? I see you haven't cast any votes yet. Just click the up vote arrow next to their score. It just takes a second – but this way you'll pay it forward to others. –  slhck Jan 20 '13 at 15:39
    
I didn't knew this, I just did that! :-) –  maneeshshetty Jan 21 '13 at 4:45
add comment
for server in ...; do
    command | while IFS=, read exitstatus id message; do
        if [[ $exitstatus == "0" ]]; then
            echo "User added on $server"
        else
            echo "Failed to add user on $server: $message"
        fi
    done
done

However, check the actual exit status of command -- does it return a non-zero exit status for failure?

for server in ...; do
    if output=$(command); then
        echo "User added on $server"
    else
        echo "Failed to add user on $server: $output"
    fi
done
share|improve this answer
    
Hadn't thought of using read and assigning the variables there, nice one. –  slhck Jan 20 '13 at 13:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.