⬆️ ⬇️

Interaction of bash-scripts with the user. Part 2

, (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.

 #!/bin/bash set -e ME=`basename $0` function print_help() { echo "   test_file" echo echo ": $ME options..." echo ":" echo " -c   test_file." echo " -w text     text." echo " -r   test_file." echo " -h ." echo } function create_file() { touch test_file } function write_to_file { echo "$TEXT" >> test_file } function remove_file { rm test_file } #     ,  . if [ $# = 0 ]; then print_help fi while getopts ":cw:r" opt ; do case $opt in c) create_file; ;; w) TEXT=$OPTARG; write_to_file ;; r) remove_file ;; *) echo " "; echo "    $ME -h"; exit 1 ;; esac done 


So what do we have?



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.

 #!/bin/bash #    PS3='  : ' select OS in "Linux" "Windows" "Mac OS" "BolgenOS" do echo echo "  $OS!" echo break done 




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.

 #!/bin/bash NAME=`basename $0` TIME=`date +%F\ %H:%M:%S` TYPE='<info>' echo "$TIME $NAME: $TYPE Operation completed successfully" >> /tmp/log 


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!

 #!/bin/bash setleds -D +caps < /dev/tty7 sleep 1 setleds -D -caps < /dev/tty7 


The script must be run with the rights of root!



Sound signals



Sound signals can be given in several ways:



The first two methods did not work for me, most likely due to the terminal settings.



Opening / closing sidiroma



 #!/bin/bash #   eject #   eject -t 


“This is not an interface!” - you will say. But the facts prove the opposite .

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



All Articles