LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-04-2008, 09:58 PM   #1
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
Bash scripting - find command with variable substitution and quoting


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
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-05-2008, 05:32 AM   #2
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Try:

Code:
eval "find / -type f $MY"

Last edited by radoulov; 12-05-2008 at 05:34 AM.
 
2 members found this post helpful.
Old 12-05-2008, 06:31 AM   #3
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Original Poster
Rep: Reputation: 232Reputation: 232Reputation: 232
Quote:
eval "find / -type f $MY"
Whoa - I've been looking at eval but dismissed it as I must have made a stupid mistake with quoting or escaping quotes in my string. I now feel a bit stupid for missing this trick. Initial experiments look good which now means my project is back on track. Funny how stupid mistakes can mean overlooking a viable and perhaps obvious solution.

I had almost given up on Bash and was considering a python os.walk/dirs.remove generator routine to programatically achieve the desired result. This meant my Bash script so far was a waste of time. Now this is not the case.

radoulov - thank you very much for taking the time to help out with this I really appreciate it. Thanks again.
 
Old 12-05-2008, 07:00 AM   #4
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Original Poster
Rep: Reputation: 232Reputation: 232Reputation: 232
Update

Right - this is now all working with a simple update to the script. All's right with the world again !

This is part of a script to track filesystem changes in a Linux system for auditing purposes and should I ever make it public (it's early days yet) , which hopefully I eventually will, I'll be sure to credit radoulov in my comments..

Also I will be looking deeper into the workings of Bash's eval as I think I've missed a trick or two here !
 
Old 07-29-2022, 08:28 AM   #5
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 138

Rep: Reputation: 19
Quote:
Originally Posted by bgeddy View Post
Code:
MY="/home/*"
find / -type f -wholename "$MY"  # works OK
MY='-wholename "/home/*"'
find / -type f $MY               #  doesn't
In the second case, "/home/*" is not expanded by bash because it is quoted, and then it's not fit by find because it considers "'s part of the name. See also https://www.linuxquestions.org/quest...ur-4175715194/.

If you use:
Code:
MY='-wholename "/home/*"'
find / -type f "$MY"
then '-wholename "/home/*"' is not expanded and it is an "unknown predicate".

However,
Code:
MY="/home/*"
OPTION=-wholename 
find / -type f $OPTION "$MY"
works.

Last edited by doru; 07-30-2022 at 01:38 AM. Reason: final completion
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH scripting -- command saved as variable bioinformatics_guy Linux - Newbie 10 09-09-2008 08:24 PM
bash script command substitution and quoting brian4xp Linux - Software 8 02-05-2008 11:43 AM
BASH command substitution that starts with a pipe | Kristofer Programming 4 02-27-2007 05:52 PM
Bash Command Substitution dakensta Programming 5 11-30-2006 03:10 PM
Bash variable substitution daYz Programming 3 04-14-2006 01:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration