I was creating a shell script that will automate some certain tasks. I had written the scripts that would compare data from two files. The logic was the extract the data from the files and then compare using the diff tool.
Now once I had developed the tool, the requirement was to make it more user friendly and add some kind of GUI so that it is easy to operate for the general users.Though I would prefer to work with the command line rather than the GUI option, I gave it a shot and searched for the options to implement some GUI features with the shell script. I came across Zenity.
I am sharing some of the code that I implemented with Zenity for user inputs and other display information. I have posted this project on Github, you can use to learn more about it and you are welcome to modify the code as per your requirements.
1. Displaying Error when Directory is not present
##Check if all required files and directories are present if [ ! -d "$data" ] || [ ! -d "$postcheck_logs" ] || [ ! -d "$precheck_logs" ] || [ ! -d "$reports" ] ; then zenity --error \ --text="Directories missing. Please reinstall or see the help doc." fi
2. Displaying a file as text information using zenity. It supports html
zenity --text-info \ --width=600 \ --height=600 \ --window-icon=$ICON\ --title="My Custom Tool" \ --filename=$INFO \ --html=TRUE\ --checkbox="I read and accept the terms." case $? in 0) rpt_select_activity ;; 1) echo "Exit" ;; -1) echo "An unexpected error has occurred." ;; esac
3. Selection Dialogue with Zenity
activity=$(zenity --list --text "Please select the option" --radiolist --column "Select" --column "Activity" FALSE "Option 1" TRUE "Option 2" ); case "$activity" in *"Option 1"* ) option 1_function;; *"Option 2"* ) option 2_function;; * ) echo "Error...";; esac
4. Open a File in an Editor Window
File_LIST=`zenity --file-selection --title="Please select the File"`; if [ "$?" != 0 ]; then zenity --error --text="No File selected."; exit 1; fi zenity --text-info \ --width=600 \ --height=600 \ --title="Please confirm the selected File" \ --editable=TRUE\ --filename=$File_LIST \ --checkbox="The selected File is correct" case $? in 0) echo "RNC Selected" ;; 1) echo "Exit" ;; -1) echo "An unexpected error has occurred." ;; esac
I mostly used the above options with my tool which helped me add some of the GUI features to my shell script. It is a good way to make your scripts bit more interactive.