If you have developed bash scripts with user interface , I’m sure you thought how “basic” they look in the terminal with some ASCII characters like “-” and “+” to provide a graphical so called menu. If you wanted to enhance your scripts user interface and make them more user friendly, you should take a look at the “dialog” frame work for creating such user interfaces. If you are an Ubuntu user you can easily get it by just typing sudo apt-get install dialog from your distros repository. Also you can download it from http://www.hightek.org/dialog/ as a tar ball and install it on your system.
To take a brief look at the capabilities of this dialog package we can list them as following:
–title: Lets you set the title of the dialog box.
–backtitle: Sets the title in the background for the main background frame. (Possibly you can set it as your company’s name)
–yesno: Creates a basic Yes or No menu for you to verify script actions with the user.
–msgbox: As you can guess from the name, it is a basic box with some info that you want to display to users.
–infobox: Nice message box version for letting your users that there are some processes are going behind the scenes.
–inputbox: A message box where you can take text input from the user.
–textbox: This box is sort of a ”cat’ command that will display a files content.
–checklist: If you want your users to select multiple options from a menu you can use this option.
–radiolist: Works similar to checklist option with some addition as to display selected options.
–menu: As the name implies, a nice menu layout where your user can select his preferences.
–gauge: This option brings a “process” bar to the screen in which you can implement in a recursive call to display percentage wise process of your script’s process.
–file: Displays the contents of a directory with its subdirectories and files.
–stdout: In my honest opinion, this is the most critical option that you would like to implement in your script for dialog. It lets you get the user selections as inputs to your variables.
If you want more details about options and how you can use them including some nice coloring effects such as changing the background color or text colors you can always refer to man page for dialog.
So, how can you implement this package into your scripts? I will go through a really sort example. Lets take the case that you have a git repository which is maintained by an admin using ”gitolite” and all the other users have admin rights into the git repository so that they can handle their own repository creating processes. However, you don’t want your users to randomly wander around your administration repository and you want to automate this process. You can use this following bash script to implement such an application. Before I start, I would like to remind you, you need to have ”git’ package and “dialog” package in each user’s computer. You can get them as the following on an Ubuntu environment:
sudo apt-get install git dialog
First lets start with our script backbone. You can also get the code from my public git repo at Github from the following link:
#Color Codes BOLD=" 33[1;39m" NORMAL=" 33[0;39m" GREEN=" 33[32m" RED=" 33[31m" #Set a default repo server address and also give the change #to the user to change address if he likes default_git_server=git@yourserver #Creates the dialog box git_server=`dialog --stdout --title "Server Address" --backtitle "Egemen Inc." --inputbox 'Please enter the git server address' 0 0 $default_git_server` #Quit the script if the user selects "Cancel" if [[ $git_server == '' ]]; then echo; echo -e $BOLD$RED"You left the repo name setting script."$NORMAL ; exit; fi #Gets the name of the repo name that user wants to create git_repo=`dialog --stdout --title "New Repo Name" --backtitle "Egemen Inc." --inputbox 'Please enter the new git repo name that you would like to add' 0 0` #Quit the script if the user selects "Cancel" if [[ $git_repo == '' ]]; then echo; echo -e $BOLD$RED"You left the repo name setting script."$NORMAL ; exit; fi #Gets the description of the repo that user wants to create #It is really usefull if you are using a web interface to browse #your repos like GitWeb repo_desc=`dialog --stdout --title "Repo Description" --backtitle "Egemen Inc." --inputbox 'Please enter the new git repo description' 0 0` #Quit the script if the user selects "Cancel" if [[ $repo_desc == '' ]]; then echo; echo -e $BOLD$RED"You left the repo name setting script."$NORMAL ; exit; fi echo; #This part is git related. Script clones the gitolite's admin #repo into the system, makes the necessary changes #And pushes it back to the admin repo with commit description. #Also sets the visibility options if you are using gitweb. After the #process it deletes the gitolite admin repo from the user's hard-drive. cd /tmp git clone $git_server:gitolite-admin cd gitolite-admin/conf echo >> gitolite.conf echo "repo $git_repo" >> gitolite.conf echo " RW+ = @all" >> gitolite.conf echo " R = gitweb daemon" >> gitolite.conf echo " $git_repo = "$repo_desc"">> gitolite.conf git commit -a -m "New repo: $git_repo has just been added." git push rm -rf /tmp/gitolite-admin
I think that comments in the code are pretty self explanatory. I will just go through a quick walk through, the script first gets the git server address where the repos are created and stored. Then it asks user to enter the name for the new repo that they want to create. Also we get the repo description for the sake of completion. In this script, we set the user rights to the new repo as @all which means all the verified users in that git repo environment can reach this newly created one. After getting the user input, we pull the gitolite-admin repo (gitolite is what I use for administrating git repos in our local network.) and make the necessary changes in its config file and then commit the changes and push it back to the server. After this process we clean out the gitolite-admin repo from the user’s computer. Note that if you are using some other git repo administration tool other than gitolite, you might need to adjust the configuration file syntax part.
Another tip about dialog framework is that, when a user selects “Cancel” button from the menus, –stdout returns a empty string, as in the above script you can control this by comparing the return value with ”. This way, your script can quit its process at any given time. Also in –menu option, I saw that “cancel” button returned “$” so, that was what I used to control menu box’s cancel option. It is useful to check all the possible return values from dialogs so that you can develop a more stable script.
I have used a simple flow for the script, however, I would suggest you to use functions for readability and allowing your script to have some options like help and and configuration. Also, keep it in mind that the order of the dialog’s options are important in the sense of input that they wait. You noticed that for height and weight inputs I always used “0” so that my dialog boxes will have as much size as they want. You can set a size for them if you like. I will also include the following dialog box screens that you should be getting if you use my sample script: