I am trying to do an IF statement from the output of an executed commmand. Here is how I am trying to do it, but it doesn't work. Does anyone know the right way to do this?

if [ "`netstat -lnp | grep ':8080'`" == *java* ]; then
  echo "Found a Tomcat!"

6 Answers 6


Use the bash [[ conditional construct and prefer the $(<command>) command substitution convention. Additionally, [[ prevents word splitting of variable values therefore there is no need to quote the command substitution bit..

if [[ $(netstat -lnp | grep ':8080') = *java* ]]; then
  echo "Found a Tomcat!"

Another alternative is to simply test the exit status of grep itself, which will return false (1) if there was no match and true (0) if there was one, by not using the [ command.

if netstat -lntp | grep ':8080.*java' > /dev/null; then
    echo "Found a Tomcat!"

The redirection to /dev/null is to prevent it from also printing the found line to the screen.

  • 21
    Note the -q option to grep instead of redirecting to /dev/null. With -q, grep doesn't output anything and stops searching after the first match. Oct 24, 2012 at 22:37

Even more simple,

netstat -lntp | grep ':8080.*java' > /dev/null && command

If you just want to do one thing.


You can do more precise, yet simple, matching with awk.

if netstat -lnp | awk '$4 ~ /:8080$/ && $7 ~ /java/ {exit(0)} END {exit(1)}'; then …

To match the structure of your command more closely, the portable way of doing wildcard matching on a string in a shell is with the case construct.

case "$(netstat -lnp | grep ':8080')" in
  *java*)  echo "Found a Tomcat!";;

Another real-world example :)

if [[ $(HEAD mycompany-intranet.com | grep '200\ OK' | wc -l) = "1" ]];  then
    echo doing some intranet settings (proxy, etc)
    echo doing some work-at-home settings (proxy, etc)
  • What does HEAD do? Is that a custom function or alias on your system?
    – luckman212
    Apr 13, 2019 at 20:46
  • usr/bin/HEAD → lwp-request. And lwp-request appears to be this one. Not sure, how I came to it (not manually for sure), of if it's all there by default. There appears to be a lowercase usr/bin/head as well. Apr 14, 2019 at 9:50

Here's how I did it in a script that determines whether my music player is open or not, and if a song is playing if it is open.

rbstatus=$(rhythmbox-client --print-playing)

pgrep -x rhythmbox >/dev/null && if [ "$rbstatus" = "-" ]; then
   echo "No music playing"
   rhythmbox-client --print-playing
fi || echo "Music player is Closed."
  • Welcome on U&L! A side note: consider that &&/|| is not equivalent to if/else. Your code will print "Music player is Closed." even if pgrep succeeds and the command in the else block fails (returns a non-zero status) for some reason (likely not a real issue in your case, of course).
    – fra-san
    Aug 29, 2020 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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