LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   bash - quoting (https://www.linuxquestions.org/questions/linux-general-1/bash-quoting-492413/)

RGummi 10-14-2006 03:43 PM

bash - quoting
 
Hello,

I have some problems in using quotes:

I want to use the dialog command in a script so that the user can select some options.

This works fine
Code:

index=0
str=""
valarray_tag=(1 2 3)
valarray_desc=('This_is_option_1' 'This_is_option_2' 'This_is_option_3')

for item in ${valarray_tag[@]}; do
        str=$str$item" "${valarray_desc[$index]}" "
        index=$((index+1))
        done
str=${str%' '}
dialog --menu "Test" 0 0 30 $str 2>/tmp/$$.dialog

But if I have descriptions with spaces I get in trouble!
Code:

index=0
str=""
valarray_tag=(1 2 3)
valarray_desc=('This is option 1' 'This is option 2' 'This is option 3')

for item in ${valarray_tag[@]}; do
        str=$str$item" "${valarray_desc[$index]}" "
        index=$((index+1))
        done
str=${str%' '}
dialog --menu "Test" 0 0 30 $str 2>/tmp/$$.dialog

By directly typing I can quote the string but how is this done in my for loop? I have tried
Code:

str=$str$item" '"${valarray_desc[$index]}"' "
but then the ' get \' and this is not what I wanted!

Any ideas?

Thanks
RGummi

Tinkster 10-14-2006 04:36 PM

Does that do what you want?

Code:

index=0
str=""
valarray_tag=(1 2 3)
valarray_desc=('This is option 1' 'This is option 2' 'This is option 3')

ori=$IFS
IFS='\n'
for item in ${valarray_tag[@]}; do
        str=$str$item" "${valarray_desc[$index]}" "
        index=$((index+1))
        done
IFS=$ori
str=${str%' '}
dialog --menu "Test" 0 0 30 $str 2>/tmp/$$.dialog



Cheers,
Tink

RGummi 10-15-2006 09:21 AM

no!

I have already tried this. How can I add a ' to a string without getting \'?

RGummi

RGummi 10-21-2006 03:06 PM

Ok I have solved my problem in an other way! Since the shell insist on quoting special characters like ' I will use it to automatically quote the right parts. So I put all in a new array!

Code:

#! /bin/bash  -xv

index=0
valarray_tag=(1 2 3)
valarray_desc=('This is option 1' 'This is option 2' 'This is option 3')

for item in ${valarray_tag[@]}; do
        newArray[$((2*index))]=$item
        newArray[$((2*index + 1))]=${valarray_desc[$index]}
        index=$((index+1))
        done
dialog --menu "Test" 0 0 30 "${newArray[@]}" 2>/tmp/$$.dialog

It rests the question: Is there any possibility to prevent the shell from automatically quoting, escaping things???

RGummi


All times are GMT -5. The time now is 02:08 AM.