LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 10-14-2006, 03:43 PM   #1
RGummi
Member
 
Registered: Nov 2005
Posts: 90

Rep: Reputation: 15
Thumbs up 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
 
Old 10-14-2006, 04:36 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
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
 
Old 10-15-2006, 09:21 AM   #3
RGummi
Member
 
Registered: Nov 2005
Posts: 90

Original Poster
Rep: Reputation: 15
no!

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

RGummi
 
Old 10-21-2006, 03:06 PM   #4
RGummi
Member
 
Registered: Nov 2005
Posts: 90

Original Poster
Rep: Reputation: 15
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
quoting web contents in the 'member area' of company homepage -- legal or not? baikonur General 11 06-14-2006 07:42 AM
Bash Sleep vs crontab and bash serial port nutthick Programming 4 06-01-2006 02:42 AM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 05:20 PM
Quick question: quoting text in sed overbored Linux - Software 0 06-24-2004 01:23 PM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM


All times are GMT -5. The time now is 01:30 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration