Link to home
Start Free TrialLog in
Avatar of Nefertiti_IT
Nefertiti_IT

asked on

xargs/grep/expr syntax error

I need to execute a command which returns an integer and compare that result to the result of another integer-returning command and get back 0 or 1 if the integers match or not. Here's what I have:
/home/me -T B | grep -c 11 | xargs expr -I{} != /home/me -TC | grep 20081008 | grep -c 11. I'm getting a syntax error. Is what I'm trying to do possible? If so, what's the issue with my syntax?

Thanks in advance,
Nefertiti_IT
Avatar of sunnycoder
sunnycoder
Flag of India image

#!/bin/bash
res1=`/my/command1`
res2=`/my/command2`
if [ "$res1" = "$res2" ]
then
     echo match
else
     echo failed
fi
Avatar of Nefertiti_IT
Nefertiti_IT

ASKER

i'm really trying to build this into a one-liner if possible. no go?
You can do something like if [ `command1` -eq `command2` ]; then echo match; fi
but you cant do command1|command2 and then compare their return status.
to clarify further ... `` would capture the output ... If you mean values returned to shell, you would need $? to get return status of last command

command1
r1=$?
command2
r2=$?
if [ $r1 -eq $r2 ]
then
   echo match
else
   echo failed
fi
ASKER CERTIFIED SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you use the $() construct instead of backticks, you get the advantage that you can nest $(). Also I think it's easier to read, but that's only a personal opinion

[ $(/home/me -T B | grep -c 11 | xargs expr -I{}) != $(/home/me -TC | grep 20081008 | grep -c 11) ] && echo 0 || echo 1

Not sure about "expr -I{}" though, what does that do?
Thanks Tintin!