Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


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"
for server in 
command // to add user 

Script end

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


User added in
user failed to add in

Any help on this would be highly appreciated.

share|improve this question
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 
  [[ "$(command)" =~ added ]] \
    && echo "User ${user} added in ${server}" \
    || echo "User ${user} failed to add in ${server}"

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
for server in ...; do
    command | while IFS=, read exitstatus id message; do
        if [[ $exitstatus == "0" ]]; then
            echo "User added on $server"
            echo "Failed to add user on $server: $message"

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"
        echo "Failed to add user on $server: $output"
share|improve this answer
Hadn't thought of using read and assigning the variables there, nice one. – slhck Jan 20 '13 at 13:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .