[Nottingham] "unescaping" the wildcard character for xargs

Mike Cardwell nlug at lists.grepular.com
Tue Jun 19 12:16:48 UTC 2012


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

On 19/06/12 12:30, Rory Holland wrote:

> Let's say I have a command which outputs line-delimited filenames
> with wildcards in them.
> 
> For example:
> 
> /home/alice/* /home/bob/* /home/eve/*
> 
> I'm trying to cat these files, but using the following doesn't
> work
> 
> <command> | xargs cat
> 
> This is because the * character is being interpreted literally, so
> I get the following error on each line:
> 
> cat: /home/alice/* : no such file or directory.
> 
> Nothing in man xargs seems to help. What can I do?

The problem is, it's the shell which interprets and translates the
"*", not cat. Under normal circumstances, if you were to run the
command: "cat *", then the "*" is translated by the shell before cat
even sees the argument list.

I don't know how you can compensate for this in xargs, and I'm sure
there's a better way than this, but I'd just do it like this:

<command> | awk {'print "cat "$1'} | sh

As long as the output of <command> is really as simple as
"/home/user/*" and wont contain odd characters like spaces or
backslashes etc, then you'll be safe, otherwise you'll have to
consider properly escaping things.

- -- 
Mike Cardwell  https://grepular.com/     http://cardwellit.com/
OpenPGP Key    35BC AF1D 3AA2 1F84 3DC3  B0CF 70A5 F512 0018 461F
XMPP OTR Key   8924 B06A 7917 AAF3 DBB1  BF1B 295C 3C78 3EF1 46B4
-----BEGIN PGP SIGNATURE-----

iQGGBAEBCgBwBQJP4G4HMBSAAAAAACAAB3ByZWZlcnJlZC1lbWFpbC1lbmNvZGlu
Z0BwZ3AuY29tcGdwbWltZTgUgAAAAAAVABpwa2EtYWRkcmVzc0BnbnVwZy5vcmdt
aWtlLmNhcmR3ZWxsQGdyZXB1bGFyLmNvbQAKCRCdJiMBwdHnBMgyCACOdSNnjQwt
ruxoYwR8+gQtdqX5sKuRkh3GVRUvg10v9iMB9LPd5Sm305GKIWpc+zAF+qm9Tipi
nEd6+6TTu5Mi4zHKnoOj2Wge+wPEBtg2eHZljHhiAXx7lhaVCeT5vNwNXMYce7/C
6/4GFZjVHM01b5HW1sa1necyag1GfCdPXXOI6ApfkgsCg5rXwjDFeoU4T4MB4hwb
fKQxTMsey0qRzUcE1BU7vEwCqkwzrvcHS1dzYPa8QJsdlS2s/3h55GpZ7Nc0wap0
x1h7tPYPtlsaDV5T3wR/osor3XFXk/TciQcI2FnmyQvJRCmIMtz9qfF8IVR5pXa2
WqMQv9pa1JvS
=2L+t
-----END PGP SIGNATURE-----



More information about the Nottingham mailing list