LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] Shell quoting mystery

[ILUG] Shell quoting mystery

Andrew glug at lunch.za.net
Sat May 24 12:42:27 IST 2008


On Friday 23 May 2008 15:40:32 Lars Hecking wrote:
>  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.*.
If it does not get quoted, then it will get expanded to a list of files, which 
is not what find requires.
>
> 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 ')'
>
>  I tried all sorts of quoting tricks, but can't get rid of the single
> quotes surrounding core.\*. Any ideas?
For the find command, you need to run the command (in part):
	find '-name' 'core.*'
without the backslash.  find will check whether each filename under 
consideration matches the wildcard supplied, and the problem you have is 
preventing the shell from expanding the wildcard while simultaneously wanting 
it to separate words.

If you want to set up the command in variables beforehand, you could do it 
like this:
  FINDRX_C[0]="-name"
  FINDRX_C[1]="core.*"
  FINDRX_C[2]="-o"
  FINDRX_C[3]="-name"
  FINDRX_C[4]="core"
# then you see your required result with:
  find "${FINDRX_C[@]}"
which with set -x says:
+ find -name 'core.*' -o -name core

At this point, you would be justified in thinking there's a better way.  I 
can't think of one, but if you are into spectacularly ugly hacks ...
	FINDRX_C="-iname corE.* -o -name core".

&:-)



More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


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!
RSS Version
Powered by Dell