Register
It is currently Thu Dec 14, 2017 6:43 pm

Programming a find command


All times are UTC - 6 hours


Post new topic Reply to topic  [ 11 posts ] 
Author Message
 PostPosted: Wed Apr 13, 2016 12:25 pm   

Joined: Thu Oct 31, 2013 5:52 pm
Posts: 11
I am flummoxed trying to figure out what I'm doing wrong. I am trying to script an exclude list for 'find' and the result looks correct to me but I'm getting this:
Code:
++ find 'Core Engineering' -type f '\(' '!' -name '\*c.json' -and '!' -name '\*r.json' -and '!' -name '\*g.json' '\)' -print
find: paths must precede expression: \(
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

The path is correct and exists so I don't understand what it is complaining about. Can someone see what I am missing? TIA.


Top
 Profile  
 PostPosted: Wed Apr 13, 2016 1:54 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
please post the command you have entered as well.
Seems like syntax error.


Top
 Profile  
 PostPosted: Thu Apr 14, 2016 5:49 am   

Joined: Thu Oct 31, 2013 5:52 pm
Posts: 11
Thanks for the reply. Here is the code segment including the loop that creates the exclude string:
Code:
string='\( '
first=1
for ex in ${EXCLUDE[@]} ; do
  if [ ${first} -eq 1 ] ; then
    string="${string} ! -name $ex"
    first=0
  else
    string="${string} -and ! -name $ex"
  fi 
done
string="${string} \\)"
list=`find "${PROJECT_PATH[$i]}" -type f -a ${string} -print`


Last edited by gw1500se on Thu Apr 14, 2016 7:44 am, edited 1 time in total.

Top
 Profile  
 PostPosted: Thu Apr 14, 2016 6:01 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
list=`find "${PROJECT_PATH[$i]}" -type f -a ${string} -print`


Top
 Profile  
 PostPosted: Thu Apr 14, 2016 6:25 am   

Joined: Thu Oct 31, 2013 5:52 pm
Posts: 11
Thanks again but that did not help. Same error.
Code:
++ find 'Core Engineering' -type f -a '\(' '!' -name '\*c.json' -and '!' -name '\*r.json' -and '!' -name '\*g.json' '\)' -print
find: paths must precede expression: \(
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]


Top
 Profile  
 PostPosted: Thu Apr 14, 2016 7:08 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
There is no need to quote the exclamation mark.
Try the statements you want to use in a script on the commandline and make it work there first.
Then add it to your script.

Your string has to look like this:
find somedir -type f -a ! \( -name hello -a ! -name hello2 \)

find wants a proper string.
You are quoting improperly:

a '\(' results after shell treatment to \( but find wants a single bracket at this place.
Single quotes suppress any shell tampering.
Double quotes allow expansion of $ expressions.


Top
 Profile  
 PostPosted: Thu Apr 14, 2016 7:43 am   

Joined: Thu Oct 31, 2013 5:52 pm
Posts: 11
That quoting is from the bash -x output. If you look at the code I posted previously, there is no quoting except for the path.


Top
 Profile  
 PostPosted: Thu Apr 14, 2016 9:33 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
Paste echo $string


Top
 Profile  
 PostPosted: Thu Apr 14, 2016 9:54 am   

Joined: Thu Oct 31, 2013 5:52 pm
Posts: 11
Code:
\( ! -name \*c.json -and ! -name \*r.json -and ! -name \*g.json \)


Top
 Profile  
 PostPosted: Thu Apr 14, 2016 10:30 am   

Joined: Thu Oct 31, 2013 5:52 pm
Posts: 11
I've decided to give up on this. I thought bash would be quicker than perl in this case but with this problem switching to perl will be easier.


Top
 Profile  
 PostPosted: Thu Apr 14, 2016 12:08 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
Your condition to find is wrong.
There will be never ever a file with a name ending on r.json AND g.json
You want file named r.json OR g.json
Adjust you logic.

You'll have the same problem in each and every programming language.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 17 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP