LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Adding dynamic command parameters in a Bash script (http://www.linuxquestions.org/questions/programming-9/adding-dynamic-command-parameters-in-a-bash-script-666167/)

haydenyoung 08-28-2008 08:39 PM

Adding dynamic command parameters in a Bash script
 
I currently have a bash script which loops the results of the find command;

Code:

for k in `find $assets -iname $j -type f`
but would like to allow users to set a no-recursive parameter which will add the -maxdepth parameter to the above find command;

Code:

...
[ "$recurse" == "0" ] && maxdepth='-maxdepth 1'
...
for k in `find $assets $maxdepth -iname $j -type f`

Unfortunately, the above yields the error;

find: invalid predicate `-maxdepth=1'

However, setting two variables;

Code:

...
if [ "$recurse" == "0" ]
then
maxdepth_param='-maxdepth'
maxdepth_val='1'
fi
...
for k in `find $assets $maxdepth_param $maxdepth_val -iname $j -type f`

works perfectly but seems extraneous.

Is this the only way to accomplish this or is there a cleaner solution.

Any help much appreciated.

ntubski 08-28-2008 09:29 PM

Code:

...
[ "$recurse" == "0" ] && maxdepth='-maxdepth 1'
...
for k in `eval find $assets $maxdepth -iname $j -type f`

should work.

ta0kira 08-28-2008 09:31 PM

Try adding eval before find.
ta0kira

PS This board needs auto refresh every 1 second ;)

haydenyoung 08-29-2008 12:20 AM

Thanks all. Yep eval worked;

Code:

...
[ "$recurse" == "0" ] && maxdepth='-maxdepth 1'
...
for k in `eval find $assets $maxdepth -iname $j -type f`



All times are GMT -5. The time now is 07:18 PM.