, (bash.org.ru)
Your attention is a new selection of means of communication scripts with the user. I hope it will be interesting to everyone who is not afraid to work with the console.
The first part can be found
here .
Options (keys)
This method was worthy of the first part of the article, but did not get there because of the forgetfulness of the author. Undoubtedly, this method is familiar to all users * nix, at least once working with the console. A simple illustrative example:
The advantage is that the keys are short and can be combined. We will try to do something like this, and in one we will explore a few more points.
So what do we have?
- The set -e command will stop the script if an error occurs during its execution (for more details on other options here ).
- The main script operations are packed into functions. Of course, it’s silly to put in functions on one team, but this is just for example, in reality they can be very well.
- The getopts function parses the passed arguments. It lists the valid options. The colon after the 'w' option means that with this option there is an additional argument that is placed in the $ OPTARG variable.
- Options can be combined, but it is worth considering that they are executed in order. This means that if we run the script -rc, then the file will be deleted first and then created. At the same time, if the file did not exist, the script will end with an error, without reaching the file creation.
- Also it is necessary to take into account the fact that after the key 'w' the argument must follow. If it is absent, the script will execute the '*' option (by default). Interestingly, if you run the script -wr Hallo , the option 'r' will be taken as an additional parameter to the option 'w', and 'Hallo' is ignored. The script will be correct -w Hallo -r
Learn more about
getopts here .
Selection
In the previous article, I considered the choice of an execution option using
case . And now let's consider creating a menu using the
select construct, which allows you to create simple, numbered menus.
Details are described
here .
Logging
It can be convenient not to display messages on the screen, but to record them in a log file. Especially if the script runs at system startup.
To do this, you can use a regular write to the file.
But there is a special logging tool -
logger .
logger Operation completed successfully sudo tail /var/log/syslog
Read more
here .
')
Desktop Alerts
Let's have a little fun and play around with the notices. To get started, put the right package:
sudo apt-get install libnotify-bin
Now let's do the simplest example right in the terminal:
notify-send --expire-time=10000 "" " "
About it already wrote on
Habré .
Keyboard Indicators
Want to blink lights on the keyboard? Yes please!
The script must be run with the rights of root!
Sound signals
Sound signals can be given in several ways:
- Using the control sequence on the system speaker
echo -e "\a"
- Using the beep utility
beep 659 120
- Console players, for example aplay, mplayer, etc.
- Speech synthesizer
The first two methods did not work for me, most likely due to the terminal settings.
Opening / closing sidiroma
“This is not an interface!” - you will say. But the facts
prove the opposite .