bug-bash
[Top][All Lists]
Advanced

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

Re: how to pass arguments with space inside?


From: lehe
Subject: Re: how to pass arguments with space inside?
Date: Fri, 10 Apr 2009 00:02:56 -0700 (PDT)

The reason why I don't use "$@" is that the arguments to the bash script is
not completely those for the executable. Some of them are just arguments
only to the bash script. So actually the script is like
[code]
#!/bin/bash
DEBUGGER=""
ARG_OPTS=""
while [[ -n "$1" ]];
        case $1 in
            --run)
                make clean
                make -j -k || exit 1
                DEBUGGER=""
                ;;
            --gdb)
                make clean
                make -j -k DEBUG=yes || exit 1
                DEBUGGER="gdb --args"
                ;;
            *)
                ARG_OPTS="${ARG_OPTS} $1"  
                ;;
        esac
        shift
done
${DEBUGGER} my_executable ${ARG_OPTS}
[/code]



Chris F.A. Johnson-3 wrote:
> 
> On Thu, 9 Apr 2009, lehe wrote:
> 
>> Sorry. I won't top post again.
> 
>    You just did!
> 
>> I tried your way but ARG_OPTS only accept the first argument and ignore
>> the
>> rest.
> 
> ARG_OPTS=( "$@" )
> 
>     All the arguments are now in the array ARG_OPTS:
> 
> printf "%s\n" "${ARG_OPTS[@]}"
> 
>> Mike Frysinger wrote:
>>>
>>> On Thursday 09 April 2009 17:47:59 lehe wrote:
>>>> Thanks Mike.
>>>
>>> please do not top post
>>>
>>>> Mike Frysinger wrote:
>>>>> On Thursday 09 April 2009 16:46:27 lehe wrote:
>>>>>> I was wondering how to pass arguments with space inside. For example,
>>>> my
>>>>>> bash script looks like:
>>>>>>
>>>>>> #!/bin/bash
>>>>>> ARG_OPTS=""
>>>>>> while [[ -n "$1" ]];
>>>>>>         ARG_OPTS="${ARG_OPTS} $1"
>>>>>>  shift
>>>>>> done
>>>>>>
>>>>>> If I pass an argument like "--options='-t 0 -v 0'", then it would be
>>>>>> splitted by the spaces inside, ie "--options='-t", "0", "-v" and "0".
>>>>>>
>>>>>> How can I achieve what I wish?
>>>>>
>>>>> use arrays
>>>>>
>>>>> $ f=( a "b c" d)
>>>>> $ printf '%s\n' "${f[@]}"
>>>>> a
>>>>> b c
>>>>> d
>>>>
>>>> Could you explain it a little? I don't quite get it. How to apply this
>>>> to
>>>> argument parsing?
>>>
>>> instead of gathering stuff into the variable ARG_OPTS, declare it as a
>>> variable and gather it there:
>>> declare -a ARG_OPTS
>>> while [[ -n $1 ]] ; do
>>>     ARG_OPTS[${#ARG_OPTS[@]}]="$1"
>>>     shift
>>> done
>>>
>>> then use it like i showed and the argument grouping will be preserved:
>>> "${ARG_OPTS[@]}"
>>>
>>> i imagine there are plenty of bash array howtos out there if you google
>>> -mike
>>>
>>>
>>>
>>>
>>
>> --
>> View this message in context:
>> http://www.nabble.com/how-to-pass-arguments-with-space-inside--tp22978918p22981193.html
>> Sent from the Gnu - Bash mailing list archive at Nabble.com.
>>
>>
>>
> 
> -- 
>     Chris F.A. Johnson, webmaster         <http://woodbine-gerrard.com>
>     ========= Do not reply to the From: address; use Reply-To: ========
>     Author:
>     Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
> 
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/how-to-pass-arguments-with-space-inside--tp22978918p22984063.html
Sent from the Gnu - Bash mailing list archive at Nabble.com.





reply via email to

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