Solved

xargs/grep/expr syntax error

Posted on 2008-10-09
7
945 Views
Last Modified: 2010-04-21
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
0
Comment
Question by:Nefertiti_IT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22680388
#!/bin/bash
res1=`/my/command1`
res2=`/my/command2`
if [ "$res1" = "$res2" ]
then
     echo match
else
     echo failed
fi
0
 

Author Comment

by:Nefertiti_IT
ID: 22680403
i'm really trying to build this into a one-liner if possible. no go?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22680442
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.
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 45

Expert Comment

by:sunnycoder
ID: 22680557
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
0
 
LVL 48

Accepted Solution

by:
Tintin earned 125 total points
ID: 22682174
A one liner to compare two commands is
[ `cmd1` -eq `cmd2` ] && echo match

Open in new window

0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 22682356
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?
0
 

Author Closing Comment

by:Nefertiti_IT
ID: 31504738
Thanks Tintin!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question