How to load a `zenity` List Dialog with space-embedded data from `ls`?
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 thels | 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
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"'