How to load a `zenity` List Dialog with space-embedded data from `ls`?
Solution 1
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[@]}"
Solution 2
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"'
Related videos on Youtube
Peter.O
Free! Free at last! ... my Windows box died in September 2010 ... and I'm free at last!
Updated on September 17, 2022Comments
-
Peter.O almost 2 years
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
-
Peter.O over 13 yearsThanks Florian... That's certianly the way! ...and it only required: ls "$zenlist"* ... much better..
-
Peter.O over 13 yearsWow!... 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 over 13 years@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'.
-
JeffG about 13 yearsGod, you're a lifesaver.