Search

Creating GUI of menu list in linux scripts

In the post we saw how to install whiptail and use it to create GUI in a linux terminal. Taking it a step further, let us see how to create menu like list like GUI in a linux terminal.

The option to be used for creating a menu is --menu, the syntax being



The value and description pair being the main part of the menu list. The value is the value that is assigned to the menu item and what follows is its description.

Example





 photo whip_menu_1.png

In the above Example A is the value Menu1 its description, B is the value and Menu2 its description. We can any number of such menu value pairs.

We can move between these menu value pairs using the arrow keys and which ever menu we select and press enter, that value of that menu item is sent to the standard error.

In the above example, if we press enter by selecting "B Menu2", value B is sent to the standard error.

To be able to store this value for futher use in a script we need to redirect the standard error to value, which can be done by swapping the file descriptors of standard output and standard error.



Which ever option is chosen the value of the same gets stored in the variable option.

Here is an example script which makes use of the menu list to get data from the user.



Give the script execute permission and run it to see the output



 photo whip_menu_2.png

 photo whip_menu_3.png


Taking password using GUI in linux script using whiptail

In the post we saw how to install and use whiptail. We can use whiptail to accept passwords from user too.

The syntax to use the password is



Example



The password entered will not be visible and will appear only as *. But by default the entered password is sent to the error console.

 photo whip_pass.png

To use the password in the script, we will have to swap the standard error and standard output consoles which can be done as below.



what ever password the user enters, it will be stored in the variable input.

Here is an example script, in which we will take the user password and verify if the same is correct or not.



Save the script, give it execute permission and run it. $ chmod 777 whiptail_password.sh $ ./whiptail_password.sh

 photo whip_pass.png

 photo whip_correct_pass.png


Taking input from user using whiptail

In the post "Creating message box in a shell script" we saw how to install whiptail and use it to create a basic message box in scrpting.

Other than just displaying messages, scripts also take input from user. Whiptail can be used to create a small GUI for the user to enter his or her input.

To create an input box we need to use the option --inputbox, the syntax being



Example



 photo whip_input.png

By default whiptail passes the input entered to the standard error console, and in a usual shell we will be able see the entered text on the terminal after we exit from the whiptail window.

But if we want to use the entered input by storing it in some variable, we will have to go about a few manipulations as there is no direct way of doing it in whiptail.

The simple manipulation we will have to do is swap the standard error console and the standard output console which can be done by appending



to the command.

Thus we can write the whiptail command as



The varialbe val will get the value that is entered in the inputbox.

Here is an example script to see the use if inputbox

whip_input.sh



Give the script execute permission and run it to see the output



 photo whip_input_tux.png

 photo whip_hello_tux.png


Creating yes no option box in linux scripting

In the post "Creating message box in a shell script" we saw how to create a message box using whiptail. Now let us see how to create a yes/no box where the user can choose one of the two and then the further action can be decided.

The syntax to create a yesno box using whiptail is



Example



 photo whiptail_yesno.png

If we select yes, the return value of the command will be 0, for no the return value will be 1. We can use the return value to decide the further course of action for the script.

Here is a script that keeps popping up the yes/no box as long as the user does not select "no" to stop the script.



$bash yesno.sh  photo whip_continue.png

 photo whip_yesno.png


Creating message box in a shell script

Shell scripts are generally assumed to be restricted to using only the command line for interaction with the user and lacks the graphical user interfaces available in the GUI mode.

One way of creating small GUIs in shell scripts is by the use of whiptail.

Here is how we can create a message box using whiptail. To create a message box we need to use the option --msgbox.

syntax:



example



The above command will create a message box of 10 X 20 with the text hello in it as shown below.

 photo whip_tail_msgbox.png

By default the text that appears for exiting the message box is OK.

We can changet the text using the option --ok-button



 photo whiptail_ok_button.png



Example script




Follow by Email