1

I am performing some actions which produce a string from the console for example I am performing the following action:

./eqagent add-target target-type=kafka events-servers=localhost:9999

and getting this message :

Equalum agent has returned the following error: The agent target already exists

I want to catch it and store it into a variable and then use it as a condition using an if statement.

mayersdesign
  • 4,231
  • 4
  • 27
  • 44
ErezN
  • 611
  • 1
  • 10
  • 23

4 Answers4

1

your question looks to me like a typical X-Y problem: your issue is that you want to act when your command failed, and when that happens, it will set the return value to a non zero value. So what you actually want is to use the $? variable. Here's a generalised example of your use case:

$ ./command
Argh... Failure!
$ echo $?
1

so you could do:

if [ $? -eq 1 ]; then
    echo "It failed :("
fi

it's likely you have several return values to descriminate errors, and thus you can check for a value equal to 2, 3 or 255 instead.

but your shell allows you to do that in a more concise way:

# prints 'it failed :(' only when command's result ($?) is non-null
./command || echo "it failed :("

so for your example you should be able to accomplish it using:

./eqagent add-target target-type=kafka events-servers=localhost:9999 || echo "it failed :("

if your command really only returns the error state using a string (not nice), you can check the exact string using:

output=$(./eqagent add-target target-type=kafka events-servers=localhost:9999)
if [ "$output" == "Equalum agent has returned the following error: The agent target already exists" ]; then
    echo "it failed :("
fi

finally you can check for substring inclusion (in case this is not the only output of your program):

error_msg="The agent target already exists"
if [ -z "${output##*$error_msg*}" ] ;then
    echo "it failed :("
fi

the above syntax works with most shells (bash, ksh, dash…).

In case you don't get the output you expect in the $output variable, which is likely if the program prints error on stderr instead of stdout, you can join both outputs using the following trick:

output=$(./eqagent add-target target-type=kafka events-servers=localhost:9999 2>&1)

finally in real use case, you might want a mix of both:

error_msg="The agent target already exists"
output=$(./eqagent add-target target-type=kafka events-servers=localhost:9999)
if [ $? -ne 0 ]; then
    echo -n "it failed "
    if [ -z "${output##*$error_msg*}" ] ;then
        echo "; because target already exists. :-s"
    else
        echo ":-("
    fi
fi
zmo
  • 22,917
  • 4
  • 48
  • 82
  • 1
    `if [ ! $? -eq 0]; then` will fail since there is no space after "0". Also why not use `if [ $? -ne 0 ]` instead? -ne = not equal – Zuzzuc Apr 25 '17 at 11:22
  • the first was a typo, and the second was with no good reason because it's how my fingers wrote it ☺ Corrected both, thank you. – zmo Apr 25 '17 at 11:24
  • Also, `output=$(./eqagent add-target target-type=kafka events-servers=localhost:9999)` will not capture any error message (assuming the error messages gets sent to stderr), you should capture it too – Zuzzuc Apr 25 '17 at 11:32
  • already covered in the part *`In case you don't get the output you expect in the $output variable, which is likely if the program prints error on stderr instead of stdout, you can join both outputs`* – zmo Apr 25 '17 at 12:23
0

Try this :

message=$(./eqagent add-target target-type=kafka events-servers=localhost:9999 2>&1)

The varname=$(some command) syntax is called command substitution, and expands to the string produced by the command contained inside the parentheses.

The 2>&1 part is a redirection that takes the output of file descriptor 2 (generally referred to "standard error") and causes it to go to the same output as file descriptor 1 (generally referred to as "standard out"). This makes sure your process substitution will collect the output of both, as a command substitution only capture standard out, not standard error.

If you simply want to test for success of failure, you can use the return code of the command, like this :

if
  ./eqagent add-target target-type=kafka events-servers=localhost:9999 2>&1
then
  # Success!
else
  # Failure...
fi

The return code is also available in the special shell variable $? if called right after the command has executed, and will vy convention be zero for success, and a non-zero value in case of failure.

Fred
  • 5,944
  • 6
  • 18
0

couldn't you do something like this:

variable=`your-command`

Then do whatever you want using $variable

AtheS21
  • 493
  • 4
  • 10
0
var="$((./eqagent add-target target-type=kafka events-servers=localhost:9999)2>&1)" # We must also make sure that stderr goes into stdin so we can capture the error message. That is what 2>&1 does
if [ $? -ne 0 ];then # Eg, it failed
    echo "the output is stored in '$var'." 
    echo "$var" > error_log.txt
fi

That should do the trick

Edit: Forgot to add stderr redirection...

Zuzzuc
  • 148
  • 9
  • the inner set of parenthesis is not useful, as you're uselessly spawning an extra shell, and are not related to pipe redirection. – zmo Apr 25 '17 at 12:25