Using the GUI in bash scripts

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

    Perhaps for you this will not be something new, but I think there will be those who come in handy.

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

    Here are the main keys:
    --calendar Display a dialog for date selection
    --entry Display a dialog for text input
    --error Display a dialog for error output
    --info Display a dialog for information output
    --file-selection Display a dialog for file selection
    --list Display list dialog
    --notification Display notification dialog
    --progress Display progress dialog
    --question Display dialog with question
    --warning Display warning
    dialog --scale Display scale dialog
    --text-info Display dialog with textual information

    Now how to use. Let's look at an example right away. For example, I wrote a little timer shutdown script. Let's 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

    #!/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








    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 doesn’t work for me).
    The result is assigned to vTime.

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


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

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

    sudo / sbin / shutdown -h $ vTime &
    Actually run the shutdown command. Here you need to pay attention to & . It is needed in order for the next task to be performed without waiting for the completion of the previous one.

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

    image

    We display in the tray the icon with the corresponding inscription.

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

    remains to create a shortcut to run the script. Naturally, you need the user to have rights to run commands through sudo. You also need to line out the line Defaults requiretty in / etc / sudoers or you can replace sudo with kdesudo.

    Here is just an introduction. I think anyone will be interested to figure out the rest of the zenity features themselves. Good luck.

    Also popular now: