7

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
  • 1
    How is it that you ran FILE=`zenity ..., yet wordcount='wc..? – muru Mar 22 '15 at 12:22
  • 1
    @muru i just copy pasted that code from a website and modified according to my script :) – Eka Mar 22 '15 at 12:46
  • case -1 is never reached as a return code cannot be negative – user3728501 Apr 17 '19 at 0:20
9

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 string wc $FILE and not actually running wc:

$ foo='wc /etc/fstab' ## WRONG
$ echo $foo
wc /etc/fstab

$ foo=`wc /etc/fstab`  ## RIGHT
$ echo $foo 
23 96 994 /etc/fstab

$ foo=$(wc /etc/fstab)   ## RIGHT
$ echo $foo 
23 96 994 /etc/fstab

In addition, in order to get only the words and not the number of characters and lines, use the -w option:

$ foo=$(wc -w /etc/fstab)   
$ echo $foo 
96 /etc/fstab

Finally, to get the number alone, with no file name, you can use:

$ foo $(wc -w /etc/fstab | cut -d ' ' -f 1 )
$ echo $foo
96
| improve this answer | |
  • Hey thanks for solving this problem..i was stuck in this error for some time. Can i ask one more question. how can i add an additional button to my form – Eka Mar 22 '15 at 12:29
  • @Eka I don't know zenity but in any case, please ask new questions by posting a new Question. – terdon Mar 22 '15 at 12:38
  • 1
    @Eka my last example shows how to get the number of words alone. If you want all the info just without the filename, the simplest is to pass it to wc as an input stream instead of a file name: wc < "$FILE" – terdon Mar 22 '15 at 12:41
6

I think that the correct code may be this:

#!/bin/bash

function count() {
  word_count=$(wc -w < "$FILE")
  zenity --info --title="Word Counted" --text="Counted words $word_count"
}

function choose() {
  FILE="$(zenity --file-selection --title='Select a File')"
  case $? in
           0)
                  count;;
           1)
                  zenity --question \
                         --title="Word counter" \
                         --text="No file selected. Do you want to select one?" \
                         && choose || exit;;
          -1)
                  echo "An unexpected error has occurred."; exit;;
  esac
}

choose
| improve this answer | |
  • 1
    FILE="$(zenity --file-selection --title="Select a File")" I think this is a syntax error. – Ismael Miguel Mar 23 '15 at 0:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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