photo
Jordan Sissel
geek

Wed, 11 Jul 2007

xargs tip

Under normal circumstances, I use this kind of xargs invocation:
xargs -n1 -I@ sh -c 'wget http://@/ | sed -e "s/^/@ /"'
The one argument passed to each invocation is replaced by '@'. This sucks if you have awkward characters such as quotes. Instead, use sh's argument processing.

# Failed invocation due to quotes:
easel(~) % echo "one\n'\"two'\nthree" | xargs -n1 -I@ sh -c 'echo "@"'
one
sh: -c: line 0: unexpected EOF while looking for matching `"'
sh: -c: line 1: syntax error: unexpected end of file
three

# Successful invocation:
% echo "one\n'\"two'\nthree" | xargs -n1 sh -c 'echo "$1"' - 
one
"two
three
The trailing - is required, because sh will set $0, $1, etc, based on those arguments. For example:
% sh -c 'echo "$0, $1"' foo bar
foo, bar
In an effort to use the shell "properly" I use $1 and pass - as the $0 argument. This lets you do neater things that the -I flag doesn't, such as multiple arguments in a given invocation.

% echo "one\ntwo\nthree\nfour" | \
  xargs -n2 sh -c 'echo $1 and $2' -
one and two
three and four
Super useful.

Comments: 0 (view comments)
Tags:
Permalink: /geekery/xargs-tip
posted at: 01:44


0 responses to 'xargs tip'


Leave a reply

You need javascript enabled to use this form. Anti-spam efforts ongoing. Also, if the comment doesn't show up, it's because the form expired. Go back and copy your comment, reload the form, and resubmit. Apologies if this is a hassle, I'm just playing with antispam methods right now. If this insists on not working, please email me about it.

Name (required)
E-mail (optional, if you want me to be able to email you back)
URL (also optional)
Comment:


Search this site

Navigation

Metadata

Home About Resume My Code (SVN)

Articles

ARP Security Dynamic DNS with DHCP OpenLDAP+Kerberos+SASL PPP over SSH SSH Security: /bin/false Week of Unix Tools Work Efficiency

Projects

fex firefox tabsearch firefox urledit grok keynav liboverride newpsm (FreeBSD) nis2ldap pam_captcha poor man's backup Solaris audio utility xboxproxy xdotool xmlpresenter xpathtool misc scripts

Presentations

Yahoo! Hack Day '06 Unix Essentials Vi/Vim Essentials

Tag Cloud

Calendar

< July 2007 >
SuMoTuWeThFrSa
1 2 3 4 5 6 7
8 91011121314
15161718192021
22232425262728
293031    

Friends

BarCamp Kent Brewster Tantek Çelik John Resig Wesley Shields Tyler Shields

Technorati