Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

xargs/grep/expr syntax error

Posted on 2008-10-09
7
Medium Priority
?
961 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

705 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