Solved

xargs/grep/expr syntax error

Posted on 2008-10-09
7
918 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now