One of the more commonly overlooked items when creating shell scripts is their ability to do more than just output to the command line. The misconception that a well written script cannot create or display more advanced GUI controls (progress bars, warning boxes, status updates, etc) will be addressed in some detail. We are going to look at several ways that you can use your shell scripts in a more user friendly manner.
Grab a Little Attention
If you use KDE (or have the KDE libraries installed even if you use Gnome), you can use a handy little utility to grab the user’s attention. The ‘kdialog’ command creates just what you would figure, a pop up dialog box to display a message in your window manager. For example:
kdialog --dontagain scriptname:nofilemsg --msgbox "Whatever You Were Looking For Ain't Here"
A Gentle Nudge
Sometimes all we need is a little nudge to let us know something we were working on is done. In the case where you have a script doing a long running task that you put in the background, you may want a quick poke once it has finished its task. Since no direct interaction is required, you can use a little tool called ‘notify-send’. This will use the desktop messaging system to send a short notice to whomever is working in the window manager. Simple syntax as follows:
notify-send "I am finally done organizing your DVDs!"
Where Do I Go Again?
In the event that we need a little more complex interaction or we just need to ‘jump around’ in our script, a simple menu can be helpful. We can accomplish that with a handy command called ‘tput’, which will allow us to build and neatly format a simple menu in our terminal window. Take the following code sample (‘tput’ is the command where you indicate what you are doing to the screen, below we are clearing the screen and then drawing something at a particular CUrsor Postion ‘tput cup’):
# clear the screen
tput cup 3 15
echo “Script Name Here”
tput cup 5 17
# Reverse video mode
echo “M E N U”
tput cup 7 15
echo “1. Add User”
tput cup 8 15
echo “2. Delete User”
tput cup 9 15
echo “3. Reboot”
# Bold mode
tput cup 11 15
read -p “Enter your Selection [1-3] ” selection
# Clear screen and reset terminal mode
You will notice that you are able then to capture the menu entry chosen in a variable called ‘selection’. You can then refer to that variable in your script using the normal methods. You can build quite complex menus and sub-menus at the terminal that can allow you to capture almost anything and react to it. I have seen entire system management tools built using just this one command to guide an administrator through a complex series of tasks.
A Little Help Please
Now all we want to do is get a quick question answered from our user. No need to require them to wait on a terminal, type some text in at the command line or pass a value in when executing the script. We can build a simple GTK Dialog Box and grab what we need in a familiar manner and happily move on. Consider the following command (which you can use in a script):
zenity --title "Feed Me a Cookie" --entry --text "Type the word COOKIE Here"
Anything entered in this dialog is passed back to either the command line (as our command above was executed directly) or, in a script, can be assigned to a variable and then referred to normally.
You can see that using just the few tools I have introduced here how you could actually build a simple shell script that does some very complex things. Create a menu for any number of tasks, update the user with status as different pieces complete, prompt the user for permission to continue and/or obtain further information based on any number of events. These are just a handful of the items you can use to give your shell scripts the GUI touch without adding a lot of overhead and additional coding time. Drop me a comment with your tips or let me know and we can cover more!