On Tue, 15 Nov 2011, Peng Yu wrote:
In any case, combining a command and its arguments in a single
string is almost always the wrong way to go about it.
Please compare the two scripts and see if the second one makes more sense.
/tmp$ cat not_convert_args_to_string.sh
#!/bin/bash
options="$2"
find $1 $options
echo find $1 $options
More sensible would be to have each option a separate argument and
do:
location=$1
shift
find "$location" "$@"
/tmp$ cat convert_args_to_string.sh
#!/bin/bash
options="$2"
cmd="find $1 $options"
eval "$cmd"
echo $cmd
See above.
/tmp$ ./not_convert_args_to_string.sh . "-type f -name '*'"
find: `./cvcd': Permission denied
find . -type f -name '*'
Use 'set -x' to see exactly what your script is doing.
--
Chris F.A. Johnson, <http://cfajohnson.com/>
Author:
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)