📜 ⬆️ ⬇️

Use GUI in bash scripts

Many Linux users sooner or later encounter bash scripts. Until recently, I did not know how to use a graphical interface in scripts. It turns out very simple.

Perhaps for you it will not be something new, but I think there will be those to whom it is useful.

We will use the zenity library (there is still a kdialog). I have it already installed. We type in the console zenity --help or man zenity.

I will give the main keys:
--calendar Display a dialog for choosing a date
--entry Display text entry dialog
--error Display a dialog for error output.
--info Display a dialog for displaying information
--file-selection Display a dialog for file selection
--list list dialog
--notification Display notification dialog
--progress Display progress dialog
--question Display a dialog with a question
--warning Display dialog with warning
--scale Display the scale dialog
--text-info Display a dialog with text information
')
Now how to use. Consider immediately an example. For example, I wrote a small script to turn off the computer on a timer.

#!/bin/bash
vTime=$(zenity --scale --title="Timer Shutdown" --text="Turn off computer after:" --step=5 --min-value=0 --max-value=120);

if [ "$?" = "1" ]; then
echo "Exit"; exit;
else
vNow=$(date +'%T');
sudo /sbin/shutdown -h $vTime &
zenity --notification --text="The system is going down in $vTime minutes. Start at $vNow!";
if [ "$?" = "0" ]; then sudo /sbin/shutdown -c;
fi
fi


Consider it in more detail.

vTime = $ (zenity —scale --title = "Timer shutdown" --text = "Turn off computer after:" --step = 5 --min-value = 0 —max-value = 120);

zenity --scale --text = "Turn off computer after:" --step = 5 --min-value = 0 —max-value = 120

image

This line displays a dialog box with a slider with a minimum value of 0, a maximum of 120 (120 minutes - 2 hours) and in increments of 5 (for some reason it does not work for me).
The result is assigned to the vTime value.

if ["$?" = "1"]; then
echo "Exit"; exit;

Variable $? contains the value 0 if the OK button is pressed and 1 if Cancel.

vNow = $ (date + '% T');
In the vNow variable, the current time.

sudo / sbin / shutdown -h $ vTime &
Actually launch the shutdown command. Here you need to pay attention to the & . It is needed so that the next task is carried out without waiting for the previous one to complete.

zenity --notification --text = "The system is going down in $ vTime minutes. Start at $ vNow! ”;

image

Display the icon in the tray with the appropriate caption.

if ["$?" = "0"]; then sudo / sbin / shutdown -c;
And we will make it possible to interrupt the shutdown by pressing the bear on the icon. If you click on the icon, zenity returns the value 0 to the variable $? ..

It remains to create a shortcut to run the script. Naturally it is necessary that the user had the right to run commands through sudo. You also need to note the line Defaults requiretty in / etc / sudoers and you can replace sudo with kdesudo.

Here is just an introduction. I think it will be interesting to anyone to figure out the rest of the zenity capabilities. Good luck.

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


All Articles