help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] Quoting in variables to be used as command args


From: suvayu ali
Subject: [Help-bash] Quoting in variables to be used as command args
Date: Tue, 5 Jun 2012 19:06:49 +0200

Hi Bash users/experts,

I have a script that uses find to look for files in a source tree. I
want to build the find command options conditionally, so I concatenate
the options as needed to a variable and then finally run the find
command with the variable as argument to get the file list.

  [[ -n ${optargs[module]} ]] && \
      findopts+=" -path *${optargs[module]}* "

  declare -a files
  files=($(find "${srcdir}" -type f ${findopts} -iregex "${regex}"))

Since the option takes a glob as an argument, I want to protect it
undergoing file expansion in case there is a matching file in $PWD. I
tried quoting the glob like this:

  findopts+=" -path \"*${optargs[module]}*\" "

  output with xtrace set: (here ${optargs[module} = roofit)
  find <srcdir> -type f -path '"*roofit*"' -iregex '<regex>'

But this does not give me the file list. But the following find command
works:

  find <srcdir> -type f -path "*roofit*" -iregex '<regex>'

I can't quote $findopts during the call to find because then find sees
it as one word. So I tried escaping the * like this:

  findopts+=" -path \*${optargs[module]}\* "

  output with xtrace set: (here ${optargs[module} = roofit)
  find <srcdir> -type f -path '\*roofit\*' -iregex '<regex>'

Again this fails to give me the file list. And yet again the following
find command works.

  find <srcdir> -type f -path \*roofit\* -iregex '<regex>'

Then I realised I can get around all this if I set noglob before the
find call, but it would be nice to know if there is a way to do this
without noglob.

Thanks for any help,

-- 
Suvayu

Open source is the future. It sets us free.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]