bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: converting array to string by quoting each element for eval


From: Chris F.A. Johnson
Subject: Re: converting array to string by quoting each element for eval
Date: Tue, 15 Nov 2011 19:43:53 -0500 (EST)
User-agent: Alpine 2.00 (LMD 1167 2008-08-23)

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)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]