Pages

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


1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete