Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm having problems loading a zenity List Dialog when the data contains spaces.

It is straight-forward when there are no spaces in the listed data,
but I don't know of a simple/standard method for space-embedded file names.

For some reason, the output of $(ls -Q /tmp) (Quoted output) still
splits the file-names at every space. The quotes and back-slashes in the
ls | sed output seem to be treated as a "finalized string" rather than
as "readable data lines" (like the first two data lines)...

I've managed to "work around the issue", but Self-Modifying code probably
isn't the best way to go! (even though it is fun! :)


Here is the method which does NOT work

zenlist="/tmp/zen list"; touch "$zenlist"
zenity --list --title='A single-column List' --width=600 --height=450 \
--column='Spaces are allowed within "q u o t e s"' \
"How much wood would a woodchuck chuck," \
"if a wooodchuck could chuck wood?" \
$(ls -Q -1 "$zenlist"* |sed 's/$/ \\/')
echo ""
# rm "$zenlist"  # Uncomment this line to delete the file

This method works, but there must be a "conventional" (better) way!?

zenlist="/tmp/zen list"
echo "zenity --list --title='A single-column List' --width=600 --height=450 \\" >"$zenlist"
echo "--column='Spaces are allowed within \"q u o t e s\"' \\"       >>"$zenlist"
echo "\"How much wood would a woodchuck chuck,\" \\"       >>"$zenlist"
echo "\"if a wooodchuck could chuck wood?\" \\" >>"$zenlist"
(ls -Q "$zenlist"* |sed 's/$/ \\/')   >>"$zenlist"
echo "" >>"$zenlist"
source "$zenlist"
# rm "$zenlist"  # Uncomment this line to delete the file
share|improve this question

2 Answers 2

up vote 5 down vote accepted

The problem is ls. It was never designed to be used in scripts. Besides, it's also pointless to use ls in scripts, because the shell can do the job much better, by simply using a glob, see http://mywiki.wooledge.org/BashGuide/Patterns

zenlist="/tmp/zen list"; touch "$zenlist" "$zenlist"$'\neven with a newline'
zenity --list --title='A single-column List' --width=600 --height=450 \
       --column='Spaces are allowed within "q u o t e s"' \
       "How much wood would a woodchuck chuck," \
       "if a wooodchuck could chuck wood?" \
       "$zenlist"*

And for a general way to put list items with spaces and other chars into a "variable", use bash arrays.

# assign some items to start with
items=( "How much wood would a woodchuck chuck," "if a wooodchuck could chuck wood?" )

# append some items
items+=( "$zenlist"* )

zenity --list --title='A single-column List' --width=600 --height=450 \
       --column='Spaces are allowed within "q u o t e s"' "${items[@]}"
share|improve this answer
    
Wow!... I'm starting to see the true power of the shell... You have given very clear examples; and the link is also excellent... Thanks ... –  Peter.O Jan 21 '11 at 0:22
    
God, you're a lifesaver. –  JeffG Jun 24 '11 at 14:05

You can pipe the list content into zenity, like

(echo "How much wood would a woodchuck chuck," ; \
 echo "if a wooodchuck could chuck wood?" ; \
 ls -Q -1 "$zenlist"* |sed 's/$/ \\/') \
| zenity --list --title='A single-column List' --width=600 --height=450 \
         --column='Spaces are allowed within "q u o t e s"'
share|improve this answer
    
Thanks Florian... That's certianly the way! ...and it only required: ls "$zenlist"* ... much better.. –  Peter.O Dec 24 '10 at 12:19
    
@Florian.. (1 month later).. Although your method got me rolling, geirha has now presented a pure bash "glob" method.. so there is actually no need fo 'ls' and 'sed'. –  Peter.O Jan 21 '11 at 0:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.