1

I have two variables as follows

SampleOutput=`some command giving output`
Status=`echo "$SampleOutput" | grep -qs "Active"`
echo $SampleOutput
echo $Status

Here $SampleOutput has value as AgentEnable=Active bla bla bla

However, $Status is coming as blank I am not sure why $Status is coming as blank when it should have value AgentEnable=Active

meallhour
  • 13,921
  • 21
  • 60
  • 117
  • 1
    I recommend you read this post https://stackoverflow.com/questions/9449778/what-is-the-benefit-of-using-instead-of-backticks-in-shell-scripts – Indent Sep 25 '18 at 08:41
  • Did you consider executing `echo "$SampleOutput" | grep -qs "Active"` on its own to see what its output is? – Ed Morton Sep 25 '18 at 13:52

1 Answers1

3

When using grep -q you don't get any output from grep. Only return status is available that you can get using:

grep -qs "Enable" <<< "$SampleOutput"
Status=$?

As per man grep:

-q, --quiet, --silent Quiet mode: suppress normal output. grep will only search a file until a match has been found, making searches potentially less expensive.

Note that if you are not using SampleOutput anywhere else then you can directly use:

some command | grep -qs "Enable"
Status=$?
anubhava
  • 761,203
  • 64
  • 569
  • 643