0

When I execute a command like this:

~/foo/bar | grep <someID>

the output is in this format:

"<someID>": "ENABLED",

The status "ENABLED" can also be something different but i want to check if my ID is ENABLED

I tried it with this:

output= ~/foo/bar | grep <someID>
echo $output

if [[ $output =~ .*ENABLED.* ]]; then  // Method 1
echo "is enabled"
else
echo "not enabled"
fi

case "$output" in                      // Method 2
*ENABLED*)
echo "is enabled"
;;
esac

but i always get that it is not enabled.

Basically what i want to do is to check if the return of grep contains the word "ENABLED".

Any ideas?

Helyx
  • 329
  • 1
  • 5
  • 17
  • That's looks like a piece of JSON so it should be processed with a JSON tool like `jq` rather than a plain text tool like `grep` – that other guy Nov 05 '17 at 17:53

4 Answers4

0

In straight-forward way:

some_id=2    # for example
if ~/foo/bar | grep -q "$some_id"'": *"ENABLED"' ; then 
    echo "is enabled"
else 
    echo "not enabled"
fi
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • still not working. But I did a mistake the output format has a "-0" attached after the id,it looks like this: "-0": "ENABLED", – Helyx Nov 05 '17 at 17:16
  • @Helyx, if you have made a mistake - then change the pattern appropriately to `"$some_id"'"-0: *"ENABLED"'` – RomanPerekhrest Nov 05 '17 at 17:25
  • I am kind of worried about the interpolation of regex pattern. it assumes certain format of `some_id` to behave appropriately. – Jason Hu Nov 05 '17 at 17:29
  • @Helyx, also, if there's an error in your question, please click the "Edit" link under your question and correct the error, so that future answers are closer to what you're looking for. – ghoti Nov 05 '17 at 17:38
0

you need to put backticks in your first line of code. if you did not do that, then that might be your mistake, because $output is undefined therefore empty string.

output=`~/foo/bar | grep <someID>`
Jason Hu
  • 6,239
  • 1
  • 20
  • 41
0

Both your strategies for checking the content of $output would work if you just got the appropriate data into the variable. There are multiple ways to get command output into a variable. The following are all functionally equivalent.

output=`~/bin/foo | grep "\"<someID>\""`       # deprecated -- use $(...) instead

output="$(~/bin/foo | grep "\"<someID>\"")"    # may result in multiple lines of output

read -r output < <(~/bin/foo | grep "\"<someID>\"")  # uses "Process Substitution"

The first two of these will work in any old POSIX shell. The first one even works in csh/tcsh. The last one, using Process Substitution, is bash-only (or at least, not POSIX).

But as you've seen in another answer, you probably don't need a variable. You can use a regex to match the whole line that you're interested in:

if ~/foo/bar | grep -q "\"$someID\""'": *"ENABLED"'; then 
  echo "enabled"
else 
  echo "disabled"
fi

This construct is safer if there's the possibility that $someID might appear multiple times in your output, with different values.

If you use a case statement, then you'd want to make sure you know what to do if $someID turns up multiple times.

while read -r output; do
  case "$output" in
    *ENABLED*) echo "enabled" ;;
    *DISABLED) echo "disabled" ;;
    *) echo "ERROR: Unknown state." >&2 ;;
  esac
done < <(~/foo/bar | grep "\"$someID\"")
ghoti
  • 45,319
  • 8
  • 65
  • 104
0

Try something like this:

some_command > /tmp/some_file.tmp
if grep -q "Some_Text" "/tmp/some_file.tmp";
then
echo "These are not the stormtroopers you are looking for."
else
echo "Stay gold, ponyboy."
fi

# Removing the tmp file is optional, but I'm OCD, so...
rm /tmp/some_file.tmp

This command will for-sure work on a Ubuntu system out of the box, or any *nix system with BASH terminal. You can adapt for your own usage.

Inspired by https://stackoverflow.com/a/28360230/4272202

Jonathin
  • 127
  • 1
  • 12