, , ( )
A rare script is deprived of the need to communicate with the user. We expect that the program (utility) will do what we want from it. Consequently, we need tools to influence them, and the program itself must explain how its work is progressing.
With this topic, I want to consider several ways of interacting bash-scripts with the user. The article is intended for beginners in scripting, but I hope experienced people will also find something interesting for themselves.
The topic is also provided with primitive examples that do not carry a semantic load, but allow you to see some interesting pieces in the work.
Variables
The most common way to store initial data is variables. At the very beginning of the program, several such variables are declared, in which the user writes some initial data.
This method is good if there is not much data and the script is designed for automatic execution without user intervention. It is necessary to clearly inform the user about what and where he needs to enter. It is advisable to collect all this in one place - the
configuration file . You can connect it with the
source command. For example, if the configuration file will be in the same directory as the script, we will get:
In the
config.cfg file, do not forget to place the line
EMAIL=example@gmail.com
Command line parameters
Another way to communicate data to a program is to specify it on the command line when starting. These parameters are contained in variables with numbers. For example:
$ 0 is the name of the script,
$ 1 is the first parameter,
$ 2 is the second parameter, and so on. There are also two auxiliary variables:
$ # contains the number of arguments passed;
$ @ contains all arguments passed to the script, separated by spaces.
')

Questions and confirmations

I think many people are familiar with the question from the screenshot above. Such a dialogue can be used ... well, you yourself guessed where it can be used.
Please note that in the screenshot the letter “D” is big. This means the default action, that is, if the user does not enter anything, then this will be equivalent to entering "D".
OK / FAIL
Another way a program communicates with a user is execution statuses. Most likely they are familiar to you.

The implementation is also quite simple.
This is how the script works:

Good people wrote an extended version of the script with logging and progress. I will gladly share the
link .
Based on the above link, the code can be simplified.
Pseudography
For lovers of graphical representation, there is a handy tool:
dialog . By default it is not in the system, so fix the situation.
sudo apt-get install dialog
You can try it out with a simple command:
dialog --title " " --msgbox "\n - !" 6 50
Here is an example of a progress dialogue:
Do not forget to insert
clear to clear the screen so as not to leave a blue background. This utility supports many more types of dialog boxes. The main disadvantage is that by default it is not in the system.
An alternative to
dialog can be
whiptail , which is even present on some systems by default.
More information can be found on the links:
http://unstableme.blogspot.com/2009/12/linux-dialog-utility-short-tutorial.htmlhttp://www.cc-c.de/german/linux/linux-dialog.phpGUI
Although there are ardent opponents of the
GUI , but he clearly has a right to exist. Such dialogs can be obtained using the
kdialog command (if KDE is the graphical shell), or
gdialog and
zenity (for Gnome).
For example, the form to enter a password:
kdialog --password ", :"
or
gdialog --password ", :"
Another example for KDE:
kdialog --question " ?" rc=$? if [ "${rc}" == "0" ]; then echo " yes" else echo " no" fi
And for Gnome:
As you can see, an obvious disadvantage of this method is attachment to a specific desktop environment. And indeed to a graphical environment, which may be absent. But, nevertheless, it may come in handy someday.
More information on the links:
http://pwet.fr/man/linux/commandes/kdialoghttp://linux.about.com/library/cmd/blcmdl1_gdialog.htmhttp://www.techrepublic.com/blog/opensource/gui-scripting-in-bash/1667PS To be continued ...
UPD: Added simplified code in the “OK / FAIL” section.
UPD2: Added an example of connecting the configuration file to the "Variables" section.
Published the
second part .