8

I'm trying to check if a machine is a ThinkPad or not using something like this:

sudo dmidecode | grep ThinkPad

I want the end result return true or false (or 1/0).

I'm thinking the solution might be something like this:

sudo dmidecode | grep -c ThinkPad | test xargs -gt 0

But I'm not sure how to properly use xargs here.

Philip Kirkbride
  • 9,816
  • 25
  • 95
  • 167
  • @don_crissti I don't see how I could use any of those answers without creating a shell script. I think this is different because answers are specifically one-liners for command line. – Philip Kirkbride Apr 17 '17 at 13:07
  • It's exactly the same - and your answer here proves that beyond any doubt - it's almost the same as _derobert_'s answer there but instead of `if...then...else` you're using `&&` and `||`. – don_crissti Apr 17 '17 at 13:14

5 Answers5

19

Just tack the exit status check after grep, it will always get the exit status from the last command of the pipeline by default:

sudo dmidecode | grep -q ThinkPad; echo $?

Use -q to suppress any output from grep as we are interested in exit status only.


You can use command grouping if you fancy, but this is somewhat redundant here:

sudo dmidecode | { grep -q ThinkPad; echo $? ;}
heemayl
  • 54,820
  • 8
  • 124
  • 141
13

If you're going to use this an shell script with an if check, just use -q as heemayl suggested:

if sudo dmidecode | grep -q Thinkpad
then
    echo "I'm a Thinkpad"
fi

Since the if block checks the command's exit status, we can rely on grep's exit status directly instead of printing $? and the comparing it to something else.

muru
  • 69,900
  • 13
  • 192
  • 292
7

Inspired by Heemayl's answer:

sudo dmidecode | grep -q ThinkPad && echo true || echo false

This will return true if ThinkPad is found by grep and false if not.

Philip Kirkbride
  • 9,816
  • 25
  • 95
  • 167
  • 1
    Philip, There's a caveat here... The portion after `||` will be executed if any of `grep` or `echo true` returns exit status non-zero... Although practically, the probability of failure of `echo` is nearly zero... – heemayl Apr 17 '17 at 16:40
1

Using test and command substitution,

test -n "$(sudo dmidecode | grep Thinkpad)" 

also,

[ -n "$(sudo dmidecode | grep Thinkpad)" ]
xae
  • 1,971
  • 16
  • 10
  • Using `test` here is completey redundant, and a common antipattern. Pretty much every Unix command sets its exit code to indicate success or failure; learn to use that instead of checking whether something was printed (which will be false in many common success scenarios, and could be true even if a command failed). – tripleee Apr 17 '17 at 17:14
0
sudo dmidecode | grep -c ThinkPad | xargs test 0 -lt

You need to rearrange the xargs and test command as also it's operands to be able to get what you want. This will return a true status if there are nonzero (>0) ThinkPad comprising lines.

Alternatively, if you want retain the operand order of test command you can do

sudo dmidecode | grep -c ThinkPad | xargs -I \{\} test \{\} -gt 0

And then, in both the commands, check for the $? variable's value to determine the fate of grep's success / failure, as follows:

if sudo dmidecode | grep -c ThinkPad | xargs -I \{\} test \{\} -gt 0; then
   echo "success hooray!"
else 
   echo nothing
fi