sophie: A cartoon-like representation of a girl standing on a hill, with brown hair, blue eyes, a flowery top, and blue skirt. ☀ (Default)
Sophie ([personal profile] sophie) wrote in [community profile] command_liners2010-11-03 01:32 pm
Entry tags:

xargs and apostrophes/spaces

If you've ever tried using xargs with a list of filenames, you've probably at some point come across errors like these:

xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option

ls: cannot access /home/sophie/directory: No such file or directory
ls: cannot access with: No such file or directory
ls: cannot access spaces: No such file or directory
ls: cannot access in/blah.txt: No such file or directory


Both of these errors are due to xargs by default interpreting some characters as special; in the first one, it won't allow apostrophes, and in the second it treats spaces as if they meant separate arguments. And the -0 switch doesn't help at all if you're using newlines.

Both of these errors can be resolved very easily: just add the -d"\n" switch before the command you want xargs to execute. This tells xargs that you don't want xargs to mess with your input at all except to treat newline characters as delimiters. This time, you should find that both apostrophes and spaces are accepted properly.
jadelennox: Senora Sabasa Garcia, by Goya (Default)

[personal profile] jadelennox 2010-11-03 03:02 pm (UTC)(link)
Timely advice. I just 5 min. ago gave up on debugging a piece of crap written by a predecessor of mine:

/usr/bin/find "$@" -name '*.xml' -exec xml sel -N "namespacewenthere" -t -m '/xpathwenthere' -v '@elementhere' -o ' ' -o '{}' '{}' ';' | sed -E 's/([^[:space:]]*) (.*)/xml tr scripts\/badlynamedtransform.xsl -s filesize="`cat "\2" | wc -c`" "\2" > includes\/\1.foxml.xml/' | sh


That had a lot of unnecessary garbage and also crapped out on the hundreds of filenames with spaces. I tried switching it to print0 with xargs -0, but had timing/precedence problems with the backtick. To make matters worse, I'm writing the code on a Debian system that's getting run on somebody else's Snow Leopard system, and our versions of find and xargs aren't compatible. Just 5 min. ago I said "fuck it, I'm doing this in perl".

*sigh*
pauamma: Cartooney crab wearing hot pink and acid green facemask holding drink with straw (Default)

[personal profile] pauamma 2010-11-03 03:23 pm (UTC)(link)
http://www.catb.org/jargon/html/J/job-security.html (But obviously, it can only go so far.)
vlion: cut of the flammarion woodcut, colored (Default)

[personal profile] vlion 2010-11-04 01:52 am (UTC)(link)
Perl. For when you just Want To Get The Job Done.