| Date: Fri, 23 May 2008 14:40:32 +0100 (IST)
| From: Lars Hecking <lhecking at users.sourceforge.net>
|
| bash 2.05b.1(1) on RHEL3.
|
| I have a script that builds a search expression for find
| and then runs find. For some reason, an expression which
| includes "*" gets quoted with single quotes and therefore
| literally looks for 'foo.*' instead of all foo.*.
|
| FINDRX_C="-name core.\* -o -name core"
| FINDRX="${FINDRX_C}${FINDRX_N}${FINDRX_X}"
| ...
| find ${d} -type d -name .snapshot -prune -o -type f \( $FINDRX \)
| ...
|
| becomes
| sh -x ...
| ...
| + FINDRX=-name core.\* -o -name core
| ++ find /home -type d -name .snapshot -prune -o -type f '(' -name 'core.\*' -o -name core ')'
that seems correct, yes. it is what you asked for.
| I tried all sorts of quoting tricks, but can't get rid of the single
| quotes surrounding core.\*. Any ideas?
broadly, the problem is yer trying to convert
a string into a list of arguments. that can
be tricky, esp. if those strings contain (as
in this case) shell metachars. quoting them
when they need to be quoted, and not quoting
them when they don't, and getting the string
split up into the desired argument strings,
can be a nightmare.
the solution is to stop doing so. since yer
using ‘bash’, I suggest using an array (i.e.,
an ordered list of strings — which is exactly
what you want in the first place):
args=( -name core.\* -o -name core )
find "$d" -type d -name .snapshot -prune -o -type f \( "${args[@]}" \)
other solutions can be devised, but the above
is a no-brainer.
cheers!
-blf-
p.s. unfortunately, array assignment syntax
(at least) differs in various Bourne-ish
shells — I don't know if arrays are in
POSIX or not? — and so, e.g., the above
would need a minor change to work with
the Korn shell (ksh(1)). and the older
Bourne-ish shells don't have arrays at
all (except for $*).
--
“How many surrealists does it take to | Brian Foster
change a lightbulb? Three. One calms | somewhere in south of France
the warthog, and two fill the bathtub | Stop E$$o (ExxonMobil)!
with brightly-coloured machine tools.” | http://www.stopesso.com
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!