📜 ⬆️ ⬇️

Bash scripts interaction with user

, , ( )

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.
 #!/bin/bash #      EMAIL=example@gmail.com echo "  : $EMAIL" 

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:
 #!/bin/bash source ./config.cfg echo "  : $EMAIL" 

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.
')
 #!/bin/bash #      for n in $@ do echo "$n" done 



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.
 #!/bin/bash echo -n "? (y/n) " read item case "$item" in y|Y) echo " «y», ..." ;; n|N) echo " «n», ..." exit 0 ;; *) echo "  .    ..." ;; esac 

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.
 #!/bin/bash SETCOLOR_SUCCESS="echo -en \\033[1;32m" SETCOLOR_FAILURE="echo -en \\033[1;31m" SETCOLOR_NORMAL="echo -en \\033[0;39m" echo -e " ..." # ,    rm test_file if [ $? -eq 0 ]; then $SETCOLOR_SUCCESS echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" $SETCOLOR_NORMAL echo else $SETCOLOR_FAILURE echo -n "$(tput hpa $(tput cols))$(tput cub 6)[fail]" $SETCOLOR_NORMAL echo fi 

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.
 #!/bin/bash red=$(tput setf 4) green=$(tput setf 2) reset=$(tput sgr0) toend=$(tput hpa $(tput cols))$(tput cub 6) echo -e " ..." # ,    rm test_file if [ $? -eq 0 ]; then echo -n "${green}${toend}[OK]" else echo -n "${red}${toend}[fail]" fi echo -n "${reset}" echo 


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:
 #!/bin/sh ( c=10 while [ $c -ne 110 ] do echo $c ((c+=10)) sleep 1 done ) | dialog --title "    " --gauge "Please wait ...." 10 60 0 clear 

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.html
http://www.cc-c.de/german/linux/linux-dialog.php

GUI


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:
 #!/bin/bash name=$(gdialog --title " " --inputbox "  :" 50 60 2>&1) echo " : $name" 

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/kdialog
http://linux.about.com/library/cmd/blcmdl1_gdialog.htm
http://www.techrepublic.com/blog/opensource/gui-scripting-in-bash/1667

PS 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 .

Source: https://habr.com/ru/post/126701/


All Articles