Unix Shell - xargs question

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2007 > xargs question





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author xargs question
RolandRB

2007-01-14, 7:20 am

I want to run "cmp" on a batch of files that fits a file pattern but I
want the return code to be displayed at the end of "cmp" processing. Is
this possible just using "xargs" as in the example below? I have tried
"cmp {} txtdir/{} ; echo \$?" but it gives a syntax error.

$ ls -1 r*.txt | xargs -t -I {} cmp {} txtdir/{}
cmp roland.txt txtdir/roland.txt
cmp roland2.txt txtdir/roland2.txt
roland2.txt txtdir/roland2.txt differ: char 1, line 1
cmp roland3.txt txtdir/roland3.txt

Stephane CHAZELAS

2007-01-14, 7:20 am

2007-01-14, 01:42(-08), RolandRB:
> I want to run "cmp" on a batch of files that fits a file pattern but I
> want the return code to be displayed at the end of "cmp" processing. Is
> this possible just using "xargs" as in the example below? I have tried
> "cmp {} txtdir/{} ; echo \$?" but it gives a syntax error.
>
> $ ls -1 r*.txt | xargs -t -I {} cmp {} txtdir/{}
> cmp roland.txt txtdir/roland.txt
> cmp roland2.txt txtdir/roland2.txt
> roland2.txt txtdir/roland2.txt differ: char 1, line 1
> cmp roland3.txt txtdir/roland3.txt


You cmp may have a verbose option.

Otherwise, you'd have to have xargs call sh, so best it to do
the whole thing with the shell:

(
set -x
for f in r*.txt; do
cmp "$f" "txtdir/$f"; echo "$?"
done
)

--
Stéphane
RolandRB

2007-01-20, 7:23 am


Stephane CHAZELAS wrote:
> 2007-01-14, 01:42(-08), RolandRB:
>
> You cmp may have a verbose option.
>
> Otherwise, you'd have to have xargs call sh, so best it to do
> the whole thing with the shell:
>
> (
> set -x
> for f in r*.txt; do
> cmp "$f" "txtdir/$f"; echo "$?"
> done
> )
>
> --
> St=E9phane


Thanks for that. What I did in the end is write a little script named
"cmprc" ("cmp" with return code) so that I could use xargs to call it
like this.

ls -1 r*.txt | xargs -I {} cmprc {} txtdir/{}

"cmprc" echoes the return code as well as the command.

http://www.datasavantconsulting.com...e/scripts/cmprc

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com