What is the best to pass an array with specially characters as command line arguments?

2011-11-06 Thread Peng Yu
Hi,

Suppose that I have a verbatim string "  a b c ( a'b | " in bash, and
I want to pass them as 6 command line arguments. I have to the
following conversion using quoteverb.sed to pass the 6 arguments
correctly to echo, which is a little bit cumbersome. I'm wondering if
there is any better way to pass the 6 arguments.

~$ cat quoteverb.sed
1s/^\s+//g
s/^\s\+//g
s/\s\+$//g
s/\s\+/\n/g
s/'/'\\''/g
s/^/'/gm
s/$/'/gm
s/\n/ /g
~$ cat main.sh
#!/usr/bin/env bash

verbatim_string="  a b c ( a'b | "

args="`echo \"$verbatim_string\" | sed -f quoteverb.sed`"

cmd="echo $args"
eval "$cmd"

~$ ./main.sh
a b c ( a'b |

-- 
Regards,
Peng



Re: What is the best to pass an array with specially characters as command line arguments?

2011-11-06 Thread Clark J. Wang
(Added back the bash list)

On Mon, Nov 7, 2011 at 11:50 AM, Peng Yu  wrote:

> On Sun, Nov 6, 2011 at 9:30 PM, Clark J. Wang  wrote:
> > On Mon, Nov 7, 2011 at 11:02 AM, Peng Yu  wrote:
> >>
> >> Hi,
> >>
> >> Suppose that I have a verbatim string "  a b c ( a'b | " in bash, and
> >> I want to pass them as 6 command line arguments. I have to the
> >> following conversion using quoteverb.sed to pass the 6 arguments
> >> correctly to echo, which is a little bit cumbersome. I'm wondering if
> >> there is any better way to pass the 6 arguments.
> >
> > v="  a b c ( a'b | "
> > a=( $v )
> > echo "${a[@]}"
>

There's a @ char here.


> >
> > And you may need to temporariliy enable the noglob option before that.
>
> Not working. See below. Also, 'echo "${a[@]}"' will only pass 1
> argument to echo, I want to pass 6 separate arguments to echo.
>
> ~$ cat main1.sh
> #!/usr/bin/env bash
>
> set -o noglob
> verbatim_string="  a b c ( a'b | "
>
> args=( $verbatim_string )
> set +o noglob
>
> echo "${args}"
> ~$ ./main1.sh
> a
>
>
>
>
> --
> Regards,
> Peng
>


Re: What is the best to pass an array with specially characters as command line arguments?

2011-11-06 Thread Peng Yu
Hi Clark,

>> > v="  a b c ( a'b | "
>> > a=( $v )
>> > echo "${a[@]}"
>
> There's a @ char here.

I see. It's my mistake.

But I want to pass the 6 short arguments instead of 1 long argument to
echo. (echo is just an example, it can be any command that accepts
multiple arguments.)


~$ cat ./main1.sh
#!/usr/bin/env bash

#set -o noglob
verbatim_string="  a b c ( a'b | "

args=( $verbatim_string )
#set +o noglob

echo "${args[@]}"

~$  ./main1.sh
a b c ( a'b |


-- 
Regards,
Peng



Re: What is the best to pass an array with specially characters as command line arguments?

2011-11-06 Thread Clark J. Wang
On Mon, Nov 7, 2011 at 12:56 PM, Peng Yu  wrote:

> Hi Clark,
>
> >> > v="  a b c ( a'b | "
> >> > a=( $v )
> >> > echo "${a[@]}"
> >
> > There's a @ char here.
>
> I see. It's my mistake.
>
> But I want to pass the 6 short arguments instead of 1 long argument to
> echo.


What do you mean by "1 long argument"?

[bash-4.2.10] # cat foo.sh
v="  a b c ( a'b | "
set -o noglob
a=( $v )
set +o noglob
for i in "${a[@]}"; do
echo "$i"
done
[bash-4.2.10] # bash foo.sh
a
b
c
(
a'b
|
[bash-4.2.10] #


> (echo is just an example, it can be any command that accepts
> multiple arguments.)
>
>
> ~$ cat ./main1.sh
> #!/usr/bin/env bash
>
> #set -o noglob
> verbatim_string="  a b c ( a'b | "
>
> args=( $verbatim_string )
> #set +o noglob
>
> echo "${args[@]}"
>
> ~$  ./main1.sh
> a b c ( a'b |
>
>
> --
> Regards,
> Peng
>