Ok - I've been banging my head for some time on this one. I am developing a Bash script using the find command and a
variable number of command line parameters to build up a find command. I want the find command to exclude an unknown number of directories passed by the user. I've got getopts and various functions handling the parsing and error checking correctly however I can't seem to get Bash to expand the arguments properly.
Here's a summarized description of the problem. Say the script is called fchanges.sh and may be called like this
Code:
./fchanges.sh -x/dir1/to/exclude -x/dir2/to/exclude -x/dir3/ -x/dirn"
I can get find to do what I want with an actual command line like this:
Code:
find / -type f ! -wholename "/dir1/to/exclude/*" \
! -wholename "/dir2/to/exclude" ! -wholename "/dir3/*" \
! -wholename "/dirn/*"
I am parsing the command line and building up a string to be passed to the find command so it ends up like this :
Code:
MY_COMMAND=' ! -wholename "/dir1/to/exclude/*" \
! -wholename "/dir2/to/exclude" ! -wholename "/dir3/*" \
! -wholename "/dirn/*" '
but try as I might I can't get this to work. There are no errors but the directory exclusion bit is getting ignored. I have tried echoing the $MY_COMMAND and it looks fine but when I try this --
Code:
find / -type f $MY_COMMAND
It seems to ignore the directory exclusion. I don't want to post the entire script as it's pretty large now and everything is working re the checks, parsing, command building etc. The variable expansion however seems to be getting messed up. As I have a variable number of arguments I have to build up a command string incrementally and can't do this :
Code:
find - type f ! -wholename "$name1" ! - wholename "$name2" ! \
! -wholename "$name3" ! -wholename "$namen"
without maybe a huge case statement covering all possibilities for a sensible number of arguments although this does work.
I'm sure this has something to do with expansion of the variables and quoting but not sure how to solve it. Any ideas - this is confusing the hell out of me !
If anyone is following this - heres a very brief test you can cut and paste and get an idea what I mean.
Code:
MY="/home/*"
find / -type f -wholename "$MY" # works OK
MY='-wholename "/home/*"'
find / -type f $MY # doesn't