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: Peng Yu
Subject: Re: converting array to string by quoting each element for eval
Date: Tue, 15 Nov 2011 19:46:14 -0600

On Tue, Nov 15, 2011 at 6:43 PM, Chris F.A. Johnson
<address@hidden> wrote:
> 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" "$@"

No. My real example use getopt. If I have each option in a separate
argument, I need to know all the possible arguments to find, which is
not a viable route.

>> /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.

How to pass the option "-type f -name '*'" correctly?

/tmp$ ./not_convert_args_to_string.sh . "-type f -name '*'"
+ options='-type f -name '\''*'\'''
+ find . -type f -name ''\''*'\'''
find: `./cvcd': Permission denied
+ echo find . -type f -name ''\''*'\'''
find . -type f -name '*'

-- 
Regards,
Peng



reply via email to

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