I am new to zenity
and I am trying to make a simple script for loading a file using zenity --file-selection
and the wc
command to get the word count of that file. I have successfully made a form which can be used to browse the file but I am unable to get any output. Can you please tell me where I am making the mistake?
My current script is:
#creates a box
if zenity --entry \
--title="Word count" \
--text="Enter file location" \
--entry-text "File path"
then
#Zenity file selection code for browsing and selecting files
FILE=`zenity --file-selection --title="Select a File"`
case $? in
0)
echo "\"$FILE\" selected.";;
1)
echo "No file selected.";;
-1)
echo "An unexpected error has occurred.";;
esac
# To show the location in the text box
if zenity --entry \
--title="Word count" \
--text="Enter file location" \
--entry-text "$FILE"
then
#word counting code
word_count='wc $FILE'
zenity --info --title="Word Counted" --text="Counted words $word_count"
fi
fi
In order to save the output of a command in a variable, you must enclose the command in backtics (
`command`
) or, better, in$()
($(command)
). You are using single quotes which means that you are saving the stringwc $FILE
and not actually runningwc
:In addition, in order to get only the words and not the number of characters and lines, use the
-w
option:Finally, to get the number alone, with no file name, you can use:
I think that the correct code may be this: