Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

xargs/grep/expr syntax error

Posted on 2008-10-09
7
Medium Priority
?
965 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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 500 total points
ID: 22682174
A one liner to compare two commands is
[ `cmd1` -eq `cmd2` ] && echo match

Open in new window

0
 
LVL 35

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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

916 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