I am doing an assignment on Linux scripting using Bin/bash etc.However, the lecture mention that if we use Zenity to complete the assignment, we will get higher mark. ( fyip, I have not learn how to use Zenity ) . I tried google and i dun know how to link them together and called them out. I tried using like -> ./name.sh but cant work.
Regards
Zenity is very easy to use. Please refer to the manual found at
https://help.gnome.org/users/zenity/stable/
Depending on which dialog you invoke in your script, the interface to interact with it is different.
But based on
https://help.gnome.org/users/zenity/stable/usage.html.en, you are expected to understand the exit code of the zenity program and react as accordingly.
After you have installed zenity into your system, you can easily invoke it by typing the program name on the command line like this
If you choose a date and click "OK", you get the following output like this
If you click "CANCEL", you get the last output shown in the console
The actual command given is
Code:
zenity --calendar; echo $?
Hence the first line shown on the screen is the standard out of "zenity --calendar". The second line after the date(if any), is the exit code of the zenity program using "echo $?"
Standard output and exit code are part and parcel of all unix system interaction between processes in unix system.
You are using a shell(probably bash) and it is a program. When you type a unix command or program to execute, the shell spawn new process to run the program and attach the standard out of the program to be displayed onto the screen. After a program is executed, you can always use "$?" to retrieve the exit code of the last program just ran within the same shell.
However there are other ways of obtaining the exit code of any program too, using conditional blocks such as "if" as such
Code:
if zenity --calendar; then echo "SUCCESS"; fi
What the above command does is if the exit code of executing the command "zenity --calendar" equals ZERO(0) which is TRUE in shell script, then SUCCESS will be echo(displayed) on the display. Any non-zero exit code in unix processes is normally interpreted as an error (unless otherwise stated by the program.
However the program with the above program is you can't do much except display it only. What you really want is to capture the standard out of the zenity program and use it for processing. So watch below
Code:
davidktw@Locutus:~$ DATE=`zenity --calendar`; if [ $? -eq 0 ]; then echo SUCCESSFULLY RETRIEVE THE DATE $DATE; else echo NO DATE FOUND; fi
SUCCESSFULLY RETRIEVE THE DATE 25/07/2014
davidktw@Locutus:~$ DATE=`zenity --calendar`; if [ $? -eq 0 ]; then echo SUCCESSFULLY RETRIEVE THE DATE $DATE; else echo NO DATE FOUND; fi
NO DATE FOUND
davidktw@Locutus:~$
The first run, I click on OK and hence I capture the date into a variable called "DATE" and since $? return me the last exit code of command run, if it equals to zero, I will echo out a string combine with the $DATE variable content. If the exit code is non-zero, I deem it as an error and instead echo "NO DATE FOUND", as shown in the 2nd command