Programmation GNU Linux Zenity

De Wiki Amis SH
Aller à la navigation Aller à la recherche



Le wiki : Accueil - Administrateur - Bureautique - Développeur - Intégrateur - Marketing - Multimédia - Objets numériques - Jeux - We make Hack


Programmation GNU Linux Zenity

Introduction

Zenity est un outil qui permet d'afficher des boîtes de dialogue GTK+ depuis la ligne de commande ou au travers de scripts shell. Il est similaire à gdialog, mais son but est d'être plus simple.
Il appartient à la même famille que dialog (affichage de pseudo boîtes de dialogue en console), xdialog et cdialog, mais il surpasse tous ces projets en ayant un nom bien plus agréable.

Installer Zenity

apt install zenity

Manuel

L'ensemble des manuels pour Zenity : https://help.gnome.org/users/zenity/
Liste pour utiliser les icônes de gnome : https://developer.gnome.org/icon-naming-spec/#names
Le manuel suivant est celui de Zenity sur Kali Linux 2018.
ZENITY(1)                                                                        General Commands Manual                                                                        ZENITY(1)

NAME
       zenity - display GTK+ dialogs

SYNOPSIS
       zenity [options]

DESCRIPTION
       zenity is a program that will display GTK+ dialogs, and return (either in the return code, or on standard output) the users input. This allows you to present information, and ask
       for information from the user, from all manner of shell scripts.

       For example, zenity --question will return either 0, 1 or 5, depending on whether the user pressed OK, Cancel or timeout has been reached. zenity --entry will output on  standard
       output what the user typed into the text entry field.

       Comprehensive documentation is available in the GNOME Help Browser.

OPTIONS
       This program follows the usual GNU command line syntax, with long options starting with two dashes (`-').

       Dialog options

       --calendar
              Display calendar dialog

       --entry
              Display text entry dialog

       --error
              Display error dialog

       --file-selection
              Display file selection dialog

       --info Display info dialog

       --list Display list dialog

       --notification
              Display notification

       --progress
              Display progress indication dialog

       --question
              Display question dialog

       --text-info
              Display text information dialog

       --warning
              Display warning dialog

       --scale
              Display scale dialog

       --color-selection
              Display color selection dialog

       --password
              Display password dialog

       --forms
              Display forms dialog

       General options

       --title=TITLE
              Set the dialog title

       --window-icon=ICONPATH
              Set the window icon with the path to an image. Alternatively, one of the four stock icons can be used: 'error', 'info', 'question' or 'warning'

       --width=WIDTH
              Set the dialog width

       --height=HEIGHT
              Set the dialog height

       --timeout=TIMEOUT
              Set the dialog timeout in seconds

       Calendar options

       --text=STRING
              Set the dialog text

       --day=INT
              Set the calendar day

       --month=INT
              Set the calendar month

       --year=INT
              Set the calendar year

       --date-format=PATTERN
              Set the format for the returned date. The default depends on the user locale or be set with the strftime style. For example %A %d/%m/%y

       Text entry options

       --text=STRING
              Set the dialog text

       --entry-text=STRING
              Set the entry text

       --hide-text
              Hide the entry text

       Error options

       --text=STRING
              Set the dialog text

       --no-wrap
              Do not enable text wrapping

       --no-markup
              Do not enable pango markup

       File selection options

       --filename=FILENAME
              Set the file or directory to be selected by default

       --multiple
              Allow selection of multiple filenames in file selection dialog

       --directory
              Activate directory-only selection

       --save Activate save mode

       --separator=SEPARATOR
              Specify separator character when returning multiple filenames

       --confirm-overwrite
              Confirm file selection if filename already exists

       --file-filter=NAME | PATTERN1 PATTERN2
              Sets a filename filter

       Info options

       --text=STRING
              Set the dialog text

       --no-wrap
              Do not enable text wrapping

       --no-markup
              Do not enable pango markup

       List options

       --text=STRING
              Set the dialog text

       --column=STRING
              Set the column header

       --checklist
              Use check boxes for first column

       --radiolist
              Use radio buttons for first column

       --separator=STRING
              Set output separator character

       --multiple
              Allow multiple rows to be selected

       --editable
              Allow changes to text

       --print-column=NUMBER
              Specify what column to print to standard output. The default is to return the first column. 'ALL' may be used to print all columns.

       --hide-column=NUMBER
              Hide a specific column

       --hide-header
              Hides the column headers

       Notification options

       --text=STRING
              Set the notification text

       --listen
              Listen  for commands on stdin. Commands include 'message', 'tooltip', 'icon', and 'visible' separated by a colon. For example, 'message: Hello world', 'visible: false', or
              'icon: /path/to/icon'. The icon command also accepts the four stock icon: 'error', 'info', 'question', and 'warning'

       Progress options

       --text=STRING
              Set the dialog text

       --percentage=INT
              Set initial percentage

       --auto-close
              Close dialog when 100% has been reached

       --auto-kill
              Kill parent process if cancel button is pressed

       --pulsate
              Pulsate progress bar

       --no-cancel
              Hides the cancel button

       Question options

       --text=STRING
              Set the dialog text

       --no-wrap
              Do not enable text wrapping

       --no-markup
              Do not enable pango markup

       --ok-label
              Set the text of the OK button

       --cancel-label
              Set the text of the cancel button

       Text options

       --filename=FILENAME
              Open file

       --editable
              Allow changes to text

       --checkbox=TEXT
              Enable a checkbox for use like a 'I read and accept the terms.'

       --ok-label
              Set the text of the OK button

       --cancel-label
              Set the text of the cancel button

       Warning options

       --text=STRING
              Set the dialog text

       --no-wrap
              Do not enable text wrapping

       --no-markup
              Do not enable pango markup

       Scale options

       --text=STRING
              Set the dialog text

       --value=VALUE
              Set initial value

       --min-value=VALUE
              Set minimum value

       --max-value=VALUE
              Set maximum value

       --step=VALUE
              Set step size

       --print-partial
              Print partial values

       --hide-value
              Hide value

       Color selection options

       --color=VALUE
              Set the initial color

       --show-palette
              Show the palette

       Password dialog options

       --username
              Display the username field

       Forms dialog options

       --add-entry=FIELDNAME
              Add a new Entry in forms dialog

       --add-password=FIELDNAME
              Add a new Password Entry in forms dialog

       --add-calendar=FIELDNAME
              Add a new Calendar in forms dialog

       --text=STRING
              Set the dialog text

       --separator=STRING
              Set output separator character

       --forms-date-format=PATTERN
              Set the format for the returned date. The default depends on the user locale or be set with the strftime style. For example %A %d/%m/%y

       Miscellaneous options

       -?, --help
              Show summary of options.

       --about
              Display an about dialog.

       --version
              Show version of program.

       Also the standard GTK+ options are accepted. For more information about the GTK+ options, execute following command.

              zenity --help-gtk

ENVIRONMENT
       Normally, zenity detects the terminal window from which it was launched and keeps itself above that window.  This behavior can be disabled by unsetting the  WINDOWID  environment
       variable.

EXAMPLES
       Display a file selector with the title Select a file to remove. The file selected is returned on standard output.

              zenity  --title="Select a file to remove" --file-selection

       Display a text entry dialog with the title Select Host and the text Select the host you would like to flood-ping. The entered text is returned on standard output.

              zenity  --title "Select Host" --entry --text "Select the host you would like to flood-ping"

       Display  a  dialog, asking Microsoft Windows has been found! Would you like to remove it?. The return code will be 0 (true in shell) if OK is selected, and 1 (false) if Cancel is
       selected.

              zenity  --question --title "Alert"  --text "Microsoft Windows has been found! Would you like to remove it?"

       Show the search results in a list dialog with the title Search Results and the text Finding all header files....

              find . -name '*.h' | zenity --list --title "Search Results" --text "Finding all header files.." --column "Files"

       Show a notification in the message tray

              zenity --notification --window-icon=update.png --text "System update necessary!"

       Display a weekly shopping list in a check list dialog with Apples and Oranges pre selected

              zenity --list --checklist --column "Buy" --column "Item" TRUE Apples TRUE Oranges FALSE Pears FALSE Toothpaste

       Display a progress dialog while searching for all the postscript files in your home directory

       find $HOME -name '*.ps' | zenity --progress --pulsate

AUTHOR
       Zenity was written by Glynn Foster <glynn.foster@sun.com>.

       This manual page was written by Ross Burton <ross@burtonini.com>.

SEE ALSO
       dialog(1)

                                                                                      December 2011                                                                             ZENITY(1)

Message du terminal

Lancer un script depuis le terminal affiche le message suivant :
Le terminal affiche le message suivant : Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged.
Pour ne pas afficher cette alerte, modifier la sortie des erreur à la fin de l'appel de la fenêtre Zenity avec la commande suivante : 2>/dev/null

Menu cliquable avec Zenity

Menu Zenity Exemple 1

#!/bin/bash
file=$(zenity --width=360 --height=320 --list --title "Menu" --text "Ouvrir un lien." --column Fichiers "Vision du web" "Lien1" "Lien2" "Lien3")

if [ "$file" = "Vision du web" ]; then
   xdg-open https://www.visionduweb.eu

elif [ "$file" = "Lien1" ]; then
   xdg-open /home/USER/Dossier/fichier.ext

elif [ "$file" = "Lien2" ]; then
   xdg-open /home/USER/Dossier/fichier.odt

elif [ "$file" = "Lien3" ]; then
   xdg-open /home/USER/

else
   exit 0

fi

Boîte de validation

Valeur de texte par défaut

result=$(zenity --title "Saisir un texte" --entry --text "Valeur par défaut.\nChanger de valeur si nécessaire." --entry-text "Valeur par défaut.")
echo "$result"

Barre de progression

Barre de progression définie par avance

#!/bin/sh

(
echo "10" ; sleep 1s
echo "# Réceptionner les mails" ; sleep 5s
thunderbird&
echo "20" ; sleep 1s
echo "# Ouvrir Vision du web" ; sleep 1s
/etc/alternatives/x-www-browser https://www.visionduweb.eu
echo "50" ; sleep 1s
echo "Ouvrir Facebook" ; sleep 1s
/etc/alternatives/x-www-browser https://www.facebook.com
echo "75" ; sleep 1s
echo "# Ouvrir DiscordApp" ; sleep 1s
/etc/alternatives/x-www-browser https://www.discordapp.com
echo "100" ; sleep 1s
) |
zenity --progress \
 --title="Tâches courantes." \
 --text="Lancement des tâches..." \
 --percentage=0

if [ "$?" = -1 ] ; then
       zenity --error \
         --text="Traitement annulé."
fi

Barre de progression dynamique

zenity --progress --pulsate
# Lancer le script de mise à jour.
apt update | tee >(zenity --progress --pulsate) >rapport.txt

zenity --progress  --title="Insertion des données" --text="$(echo Les variables sont ajoutées dans la base de données. | cut --characters=-160)" --width="450" --auto-close --auto-kill

Progression sur 20 secondes et fermeture automatique

i=0
(while [ $((i <= 100)) '=' 1 ]
   echo $i
   sleep 1
   i=$((i + 5))
done) | zenity --progress --auto-close

Changer le message à chaque action

#!/bin/bash
(
# ================================================== ===============
echo "# Running First Task." ; sleep 2
# Command for first task goes on this line.

# ================================================== ===============
echo "25"
echo "# Running Second Task." ; sleep 2
# Command for second task goes on this line.

# ================================================== ===============
echo "50"
echo "# Running Third Task." ; sleep 2
# Command for third task goes on this line.

# ================================================== ===============
echo "75"
echo "# Running Fourth Task." ; sleep 2
# Command for fourth task goes on this line.

# ================================================== ===============
echo "99"
echo "# La tâche se termine." ; sleep 2
# Command for fifth task goes on this line.

# ================================================== ===============
echo "# Terminé." ; sleep 2
echo "100"

) |
zenity --progress \
--title="Progress Status" \
--text="First Task." \
--percentage=0 \
--auto-close \
--auto-kill

if [ "$?" = -1 ] ; then
zenity --error \
--text="Update error."
fi
exit 0

Utiliser wmctrl pour afficher les messages de Zenity sur le devant de la fenêtre

 Source : https://ubuntuforums.org/archive/index.php/t-2172828.html

Copie de fichiers

# Copier tous les fichiers .txt du Bureau vers son répertoire utilisateur.
# Attention, les fichiers avec parenthèse ( ou espace ? ) provoquent des erreurs.
#!/bin/bash
cd /home/USER/Bureau/
for file in *.txt
do
echo "$file"
cp ${file} /home/USER/
sleep 0.1
done | zenity --progress --title="TXT" --text="uploading" --percentage="0"

Permettre l'annulation lors de la progression

Comment ajouter la barre de progression sur un ensemble de commandes, et, permettre l'annulation.
Source : http://bernaerts.dyndns.org/linux/331-linux-cancel-zenity-progress-dialog-right-way

Liste déroulante

Récupérer un choix depuis une liste déroulante

#! /bin/bash
selection=$(zenity --list "Option 1" "Option 2" "Option 3" --column="Choix unique :" --text="Choisir une seule option." --title="Choisir une option.")

case "$selection" in
"Option 1")zenity --info --text="Choix No1";;
"Option 2")zenity --info --text="Choix No2";;
"Option 3")zenity --info --text="Choix No3";;
esac

Ce qui semble être un bogue permet de créer une liste déroulante avec --entry (Fermer les parenthèse de --entry-text avant la fin du texte.)
zenity --entry --width=350 --height=80 --title="Nom du logiciel à utiliser" --text="Merci d'indiquer le nom du logiciel à utiliser pour editer le fichier de config" --entry-text="geany" kate gedit

Récupérer une valeur depuis une liste déroulante

 JOUR=$(zenity --list --text="Choices:"  --column="Jour" choix1 choix2 choix3); echo $JOUR

Ou encore :

JOUR=`zenity --entry --title="Jour" --text="Veuillez indiquer le jour de la semaine" Lundi Mardi Mercredi "Autre jour..."`
if [ $? = 0 ]
   then
       echo "Variable jour = ${JOUR}"
   fi

Fenêtre de gestion de fichiers

Afficher les fichiers cachés de HOME et des sous dossiers de HOME

#/bin/sh
cd ${HOME}
files=`find . |zenity --list --title "Choisir un fichier dans le HOME : " --column "Fichiers" --separator=" " --multiple`

for i in ${files}; do
   echo ${i}
done

Ouvrir la sélection de fichiers

#!/bin/bash
zOpenPath="$(zenity --file-selection)"
if [ "$zOpenPath" != "" ]
 then
  zData=$(cat "$zOpenPath")
  zNewData=$(echo -n "$zData" | zenity --text-info --editable --width 650 --height 400)
  zSavePath=$(echo -n "$(zenity --file-selection --filename="$zOpenPath" --save --confirm-overwrite)")
  echo -n "$zNewData" > "$zSavePath"
fi

Naviguer dans les répertoires

# Le choix d'un fichier est impossible. Navigue uniquement dans les répertoires sans interaction.
FILE=`zenity --file-selection --multiple --directory --separator="+++"`

Dialogue

Text

Récupérer les variables et les afficher

# Récupérer différentes variables texte et les afficher au fur et à mesure.
#!/bin/bash 
first=$(zenity --title="Your's First Name" --text "What is your first name?" --entry) 
zenity --info --title="Welcome" --text="Mr./Ms. $first" 
last=$(zenity --title="Your's Last Name" --text "$first what is your last name?" --entry) 
zenity --info --title="Nice Meeting You" --text="Mr./Ms. $first $last"

Renseigner un texte

zenity --entry --title="Renseigner un texte." --text="Zone de saisie de texte."

Récupérer le texte dans une variable

NOM=`zenity --entry --text "Entrer votre nom : "`
if [ $? = 0 ]
   then
       echo "Bonjour ${NOM}"
   fi

Info

zenity \
--info \
--text="Il est $(date +%Hh%Mm%Ss).\n\nL'heure du café." \
--title="L'heure du café" \
--ok-label="OK"

Question Oui Non

zenity --question --title="Question." --ok-label="OuI" --cancel-label="NoN" --text="Lancer le script ?"

Mot de passe

PRIVATE=`zenity --password --username`

Danger

zenity --warning --text "This will kill, are you sure?";echo $?

Erreur

zenity --error --text='Une erreur est survenue.' --title='Erreur.'

Gestion des erreurs

if [ "$?" = -1 ] ; then
zenity --error \
--text="Erreur."
fi
exit 0

Scale pour choisir un nombre

# Ici, choisir un nombre entre 2 et 100.
ans=$(zenity --scale --text "pick a number" --min-value=2 --max-value=100 --value=2 --step 2);echo $ans

Liste radios

ans=$(zenity  --list  --text "Is linux.byexamples.com helpful?" --radiolist  --column "Pick" --column "Opinion" TRUE Amazing FALSE Average FALSE "Difficult to follow" FALSE "Not helpful"); echo $ans

Liste cases à cocher

pays=`zenity \
   --list \
   --checklist \
   --width="360" \
   --height="420" \
   --title="Raison sociale de l'entreprise." \
   --text "Raison sociale de l'entreprise." \
   --column "Sélectionner" \
   --column "Pays" \
   FALSE TP_Allemagne \
   FALSE TP_Autriche \
   FALSE TP_Angleterre \
   FALSE TP_Belgique \
   FALSE TP_Bulgarie \
   FALSE TP_Chypre \
   FALSE TP_Croatie \
   FALSE TP_Danemark \
   FALSE TP_Ecosse \
   FALSE TP_Espagne \
   FALSE TP_Estonie \
   FALSE TP_Finlande \
   FALSE TP_France \
   FALSE TP_Grece \
   FALSE TP_Hongrie \
   FALSE TP_Irlande \
   FALSE TP_Irlande_de_Nord \
   FALSE TP_Italie \
   FALSE TP_Lettonie \
   FALSE TP_Lituanie \
   FALSE TP_Luxembourg \
   FALSE TP_Malte \
   FALSE TP_PaysBas \
   FALSE TP_Pays_de_Galles \
   FALSE TP_Pologne \
   FALSE TP_Portugal \
   FALSE TP_Republique_Tcheque \
   FALSE TP_Roumanie \
   FALSE TP_Slovaquie \
   FALSE TP_Slovenie \
   FALSE TP_Suede \
   FALSE TP_Suisse \
   FALSE TP_OTHER
`
if echo "$pays" | grep "Allemagne" >/dev/null 2>&1; then echo "TP_Allemagne"; else echo "Pas de correspondance pour l'Allemagne."; fi
if echo "$pays" | grep "Autriche" >/dev/null 2>&1; then echo "TP_Autriche"; else echo "Pas de correspondance pour l'Autriche."; fi
if echo "$pays" | grep "Angleterre" >/dev/null 2>&1; then echo "TP_Angleterre"; else echo "Pas de correspondance pour l'Angleterre."; fi
if echo "$pays" | grep "Belgique" >/dev/null 2>&1; then echo "TP_Belgique"; else echo "Pas de correspondance pour la Belgique."; fi
if echo "$pays" | grep "Bulgarie" >/dev/null 2>&1; then echo "TP_Bulgarie"; else echo "Pas de correspondance pour la Bulgarie."; fi
if echo "$pays" | grep "Chypre" >/dev/null 2>&1; then echo "TP_Chypre"; else echo "Pas de correspondance pour Chypre."; fi
if echo "$pays" | grep "Croatie" >/dev/null 2>&1; then echo "TP_Croatie"; else echo "Pas de correspondance pour la Croatie."; fi
if echo "$pays" | grep "Danemark" >/dev/null 2>&1; then echo "TP_Danemark"; else echo "Pas de correspondance pour le Danemark."; fi
if echo "$pays" | grep "Ecosse" >/dev/null 2>&1; then echo "TP_Ecosse"; else echo "Pas de correspondance pour l'Ecosse."; fi
if echo "$pays" | grep "Espagne" >/dev/null 2>&1; then echo "TP_Espagne"; else echo "Pas de correspondance pour l'Espagne."; fi
if echo "$pays" | grep "Estonie" >/dev/null 2>&1; then echo "TP_Estonie"; else echo "Pas de correspondance pour l'Estonie."; fi
if echo "$pays" | grep "Finlande" >/dev/null 2>&1; then echo "TP_Finlande"; else echo "Pas de correspondance pour la Finlande."; fi
if echo "$pays" | grep "France" >/dev/null 2>&1; then echo "TP_France"; else echo "Pas de correspondance pour la France."; fi
if echo "$pays" | grep "Grece" >/dev/null 2>&1; then echo "TP_Grece"; else echo "Pas de correspondance pour la Grece."; fi
if echo "$pays" | grep "Hongrie" >/dev/null 2>&1; then echo "TP_Hongrie"; else echo "Pas de correspondance pour la Hongrie."; fi
if echo "$pays" | grep "Irlande" >/dev/null 2>&1; then echo "TP_Irlande"; else echo "Pas de correspondance pour l'Irlande."; fi
if echo "$pays" | grep "Irlande_du_Nord" >/dev/null 2>&1; then echo "TP_Irlande_du_Nord"; else echo "Pas de correspondance pour l'Irlande du Nord."; fi
if echo "$pays" | grep "Italie" >/dev/null 2>&1; then echo "TP_Italie"; else echo "Pas de correspondance pour l'Italie."; fi
if echo "$pays" | grep "Lettonie" >/dev/null 2>&1; then echo "TP_Lettonie"; else echo "Pas de correspondance pour la Lettonie."; fi
if echo "$pays" | grep "Lituanie" >/dev/null 2>&1; then echo "TP_Lituanie"; else echo "Pas de correspondance pour la Lituanie."; fi
if echo "$pays" | grep "Luxembourg" >/dev/null 2>&1; then echo "TP_Luxembourg"; else echo "Pas de correspondance pour le Luxembourg."; fi
if echo "$pays" | grep "Malte" >/dev/null 2>&1; then echo "TP_Malte"; else echo "Pas de correspondance pour Malte."; fi
if echo "$pays" | grep "PaysBas" >/dev/null 2>&1; then echo "TP_PaysBas"; else echo "Pas de correspondance pour Pays-Bas."; fi
if echo "$pays" | grep "Pays_de_Galles" >/dev/null 2>&1; then echo "TP_Pays_de_Galles"; else echo "Pas de correspondance pour Pays de Galles."; fi
if echo "$pays" | grep "Pologne" >/dev/null 2>&1; then echo "TP_Pologne"; else echo "Pas de correspondance pour la Pologne."; fi
if echo "$pays" | grep "Portugal" >/dev/null 2>&1; then echo "TP_Portugal"; else echo "Pas de correspondance pour le Portugal."; fi
if echo "$pays" | grep "Republique_Tcheque" >/dev/null 2>&1; then echo "TP_Republique_Tcheque"; else echo "Pas de correspondance pour la Republique Tcheque."; fi
if echo "$pays" | grep "Roumanie" >/dev/null 2>&1; then echo "TP_Roumanie"; else echo "Pas de correspondance pour la Roumanie."; fi
if echo "$pays" | grep "Slovaquie" >/dev/null 2>&1; then echo "TP_Slovaquie"; else echo "Pas de correspondance pour la Slovaquie."; fi
if echo "$pays" | grep "Slovenie" >/dev/null 2>&1; then echo "TP_Slovenie"; else echo "Pas de correspondance pour la Slovenie."; fi
if echo "$pays" | grep "Suede" >/dev/null 2>&1; then echo "TP_Suede"; else echo "Pas de correspondance pour la Suede."; fi
if echo "$pays" | grep "Suisse" >/dev/null 2>&1; then echo "TP_Suisse"; else echo "Pas de correspondance pour la Suisse."; fi
if echo "$pays" | grep "OTHER" >/dev/null 2>&1; then echo "TP_OTHER"; else echo "Pas de correspondance pour un autre pays."; fi
# Tester si les valeurs cochées dans la liste de choix des pays se retrouvent dans la chaîne de retour.
# Deux exemples pour shell et deux exemples pour bash.
# sh
if echo "$pays" | grep "Allemagne" >/dev/null 2>&1; then echo "TP_Allemagne"; else echo "Pas de correspondance pour l'Allemagne."; fi
# sh
echo "$pays" | grep TP_Allemagne && echo OK || echo "Pas OK"
..
# bash
All="TP_Allemagne"
[[ $pays =~ $All ]] && echo OK || echo Pas OK
..
# bash
[[ $(grep "TP_Allemagne" <<<$pays) ]] && echo OK
..
langue=`zenity \
   --list \
   --checklist \
   --width="360" \
   --height="420" \
   --title="Langues utilisées." \
   --text "Langues utilisées." \
   --column "Sélectionner" \
   --column "Langue" \
   FALSE Allemand \
   FALSE Autrichien \
   FALSE Anglais \
   FALSE Arabe \
   FALSE Belge \
   FALSE Bulgare \
   FALSE Croate \
   FALSE Chinois \
   FALSE Danois \
   FALSE Ecossais \
   FALSE Espagnol \
   FALSE Estonien \
   FALSE Finnois \
   FALSE Francais \
   FALSE Gallois \
   FALSE Grec \
   FALSE Hongrois \
   FALSE Irlandais \
   FALSE Italien \
   FALSE Japonais \
   FALSE Letton \
   FALSE Lituanien \
   FALSE Luxembourgeois \
   FALSE Maltais \
   FALSE Neerlandais \
   FALSE Polonais \
   FALSE Portugais \
   FALSE Romanche \
   FALSE Roumain \
   FALSE Russe \
   FALSE Slovene \
   FALSE Suedois \
   FALSE Tcheque \
   FALSE OTHER
`
if echo "$langue" | grep "Allemand" >/dev/null 2>&1; then echo "TL_Allemand"; else echo "Pas de correspondance pour la langue Allemand."; fi
if echo "$langue" | grep "Autrichien" >/dev/null 2>&1; then echo "TL_Autrichien"; else echo "Pas de correspondance pour la langue Autrichien."; fi
if echo "$langue" | grep "Anglais" >/dev/null 2>&1; then echo "TL_Anglais"; else echo "Pas de correspondance pour la langue Anglais."; fi
if echo "$langue" | grep "Arabe" >/dev/null 2>&1; then echo "TL_Arabe"; else echo "Pas de correspondance pour la langue Arabe."; fi
if echo "$langue" | grep "Belge" >/dev/null 2>&1; then echo "TL_Belge"; else echo "Pas de correspondance pour la langue Belge."; fi
if echo "$langue" | grep "Bulgare" >/dev/null 2>&1; then echo "TL_Bulgare"; else echo "Pas de correspondance pour la langue Bulgare."; fi
if echo "$langue" | grep "Croate" >/dev/null 2>&1; then echo "TL_Croate"; else echo "Pas de correspondance pour la langue Croate."; fi
if echo "$langue" | grep "Chinois" >/dev/null 2>&1; then echo "TL_Chinois"; else echo "Pas de correspondance pour la langue Chinois."; fi
if echo "$langue" | grep "Danois" >/dev/null 2>&1; then echo "TL_Danois"; else echo "Pas de correspondance pour la langue Danois."; fi
if echo "$langue" | grep "Ecossais" >/dev/null 2>&1; then echo "TL_Ecossais"; else echo "Pas de correspondance pour la langue Ecossais."; fi
if echo "$langue" | grep "Espagnol" >/dev/null 2>&1; then echo "TL_Espagnol"; else echo "Pas de correspondance pour la langue Espagnol."; fi
if echo "$langue" | grep "Estonien" >/dev/null 2>&1; then echo "TL_Estonien"; else echo "Pas de correspondance pour la langue Estonien."; fi
if echo "$langue" | grep "Finnois" >/dev/null 2>&1; then echo "TL_Finnois"; else echo "Pas de correspondance pour la langue Finnois."; fi
if echo "$langue" | grep "Francais" >/dev/null 2>&1; then echo "TL_Francais"; else echo "Pas de correspondance pour la langue Francais."; fi
if echo "$langue" | grep "Gallois" >/dev/null 2>&1; then echo "TL_Gallois"; else echo "Pas de correspondance pour la langue Gallois."; fi
if echo "$langue" | grep "Grec" >/dev/null 2>&1; then echo "TL_Grec"; else echo "Pas de correspondance pour la langue Grec."; fi
if echo "$langue" | grep "Hongrois" >/dev/null 2>&1; then echo "TL_Hongrois"; else echo "Pas de correspondance pour la langue Hongrois."; fi
if echo "$langue" | grep "Irlandais" >/dev/null 2>&1; then echo "TL_Irlandais"; else echo "Pas de correspondance pour la langue Irlandais."; fi
if echo "$langue" | grep "Italien" >/dev/null 2>&1; then echo "TL_Italien"; else echo "Pas de correspondance pour la langue Italien."; fi
if echo "$langue" | grep "Japonais" >/dev/null 2>&1; then echo "TL_Japonais"; else echo "Pas de correspondance pour la langue Japonais."; fi
if echo "$langue" | grep "Letton" >/dev/null 2>&1; then echo "TL_Letton"; else echo "Pas de correspondance pour la langue Letton."; fi
if echo "$langue" | grep "Lituanien" >/dev/null 2>&1; then echo "TL_Lituanien"; else echo "Pas de correspondance pour la langue Lituanien."; fi
if echo "$langue" | grep "Luxembourgeois" >/dev/null 2>&1; then echo "TL_Luxembourgeois"; else echo "Pas de correspondance pour la langue Luxembourgeois."; fi
if echo "$langue" | grep "Maltais" >/dev/null 2>&1; then echo "TL_Maltais"; else echo "Pas de correspondance pour la langue Maltais."; fi
if echo "$langue" | grep "Neerlandais" >/dev/null 2>&1; then echo "TL_Neerlandais"; else echo "Pas de correspondance pour la langue Neerlandais."; fi
if echo "$langue" | grep "Polonais" >/dev/null 2>&1; then echo "TL_Polonais"; else echo "Pas de correspondance pour la langue Polonais."; fi
if echo "$langue" | grep "Portugais" >/dev/null 2>&1; then echo "TL_Portugais"; else echo "Pas de correspondance pour la langue Portugais."; fi
if echo "$langue" | grep "Romanche" >/dev/null 2>&1; then echo "TL_Romanche"; else echo "Pas de correspondance pour la langue Romanche."; fi
if echo "$langue" | grep "Roumain" >/dev/null 2>&1; then echo "TL_Roumain"; else echo "Pas de correspondance pour la langue Roumain."; fi
if echo "$langue" | grep "Russe" >/dev/null 2>&1; then echo "TL_Russe"; else echo "Pas de correspondance pour la langue Russe."; fi
if echo "$langue" | grep "Slovene" >/dev/null 2>&1; then echo "TL_Slovene"; else echo "Pas de correspondance pour la langue Slovene."; fi
if echo "$langue" | grep "Suedois" >/dev/null 2>&1; then echo "TL_Suedois"; else echo "Pas de correspondance pour la langue Suedois."; fi
if echo "$langue" | grep "Tcheque" >/dev/null 2>&1; then echo "TL_Tcheque"; else echo "Pas de correspondance pour la langue Tcheque."; fi
if echo "$langue" | grep "OTHER" >/dev/null 2>&1; then echo "TL_OTHER"; else echo "Pas de correspondance pour une autre langue."; fi

Calendrier

zenity --calendar
Ou encore :
szDate=$(zenity --calendar --text "Pick a day" --title "Medical Leave" --day 23 --month 5 --year 2008); echo $szDate
Un script pour charger un fichier de sauvegarde si existant à une date donnée.
 Source : https://www.linuxjournal.com/content/make-your-scripts-user-friendly-zenity

Notification avec une icone

zenity --notification --window-icon='/home/zencool/Bureau/green-tech.png' --text "Firefox open"

Ou encore, pour la gestion d'un programme pour indiquer si il tourne ou non.

#! /bin/bash
#lancer soft et notification
firefox & \
echo message:Firefox correctement lancé | zenity --notification --listen --window-icon='/chemin/vers/icon_ok.png' | zenity --notification --window-icon='/chemin/vers/icon_ok.png' --text "Firefox open" &
#recup du pid de zenity pour pouvoir le tuer proprement après
PID_ZEN=$!
#tant que firefox tourne, boucle avec pause de 2 s. On peut mettre autre chose que pidof, ps ax et chercher son appli par exemple, via son nom ou son pid qu'on aura récupérer auparavant.
while [ "$(pidof firefox)" ]
do
   sleep 2
done
#firefox ne tourne plus, donc on tue le zenity ok et on lance le zenity ko. Pour continuer le script, il suffira de cliquer sur l'icone ko
kill -9 $PID_ZEN
echo message:Firefox fermé | zenity --notification --listen --window-icon='/chemin/vers/icon_ko.png' | zenity --notification --window-icon='/chemin/vers/icon_ko.png' --text "Firefox down"
echo "commande à executer en sortie"
exit

Utiliser TIME

#!/bin/bash
# This script asks the user for a time, waits the specified amount of time, and shows an alert dialog.
TIME=$(zenity --entry --title="Timer" –text="Enter a duration for the timer.\n\n Use 5s for 5 seconds, 10m for 10 minutes, or 2h for 2 hours.")
sleep $TIME
zenity --info --title="Timer Complete" --text="The timer is over.\n\n It has been $TIME."

Form

Exemple de formulaire de saisie avec plusieurs champs pour changer de mots de passe

#!/bin/bash

#On crée le formulaire en stockant les valeurs de sortie dans $cfgpass :/
cfgpass=`zenity --forms \
   --title="Exemple qui tue la mort" \
   --text="Définir un nouveau mot de passe" \
   --add-entry="Nom de l'utilisateur" \
   --add-password="Ancien mot de passe" \
   --add-password="Nouveau mot de passe" \
   --add-password="Confirmer le nouveau mot de passe" \
   --separator="|"`

#Si on clique sur le bouton Annuler
if [ "$?" -eq 1 ]; then
   #On quitte le script
   exit
fi
#Sinon on continue
#On peut récupérer les valeurs des différents champs de cette façon :
echo "$cfgpass" | cut -d "|" -f1 #Nom de l'utilisateur
echo "$cfgpass" | cut -d "|" -f2 | md5sum #Ancien Mot de passe
echo "$cfgpass" | cut -d "|" -f3 | md5sum #Nouveau Mot de passe
echo "$cfgpass" | cut -d "|" -f4 | md5sum #Confirmation du nouveau mot de passe

echo "Terminé."

Exemple de fenêtre form pour créer un utilisateur avec une date d'expiration

zenity --forms --title="Create user" --text="Add new user" \
  --add-entry="First Name" \
  --add-entry="Last Name" \
  --add-entry="Username" \
  --add-password="Password" \
  --add-password="Confirm Password" \
  --add-calendar="Expires"

Exemple de formulaire d'affiliation

# Formulaire Zenity.
affiliation=`zenity --forms \
   --title="Lister les collaborateurs potentiels." \
   --text "Lister les collaborateurs potentiels" \
   --width="360" \
   --height="420" \
   --add-entry="Nom du site" \
   --add-entry="Nom de l'entreprise" \
   --add-entry="URL de l'entreprise" \
   --add-entry="Mail de l'entreprise" \
   --add-entry="URL du formulaire de contact" \
   --add-entry="Téléphone de l'entreprise" \
   --add-entry="Nom du gérant" \
   --add-entry="Prénom du gérant" \
   --add-entry="Fonction du gérant" \
   --add-entry="Téléphone du gérant" \
   --add-entry="Mail du gérant" \
   --add-entry="Notes complémentaires" 2>/dev/null
# Afficher les valeurs retournées.
echo "$affiliation" | cut -d "|" -f1 # TA_NOM_SITE
echo "$affiliation" | cut -d "|" -f2 # TA_NOM_ENTREPRISE
echo "$affiliation" | cut -d "|" -f3 # TA_URL_ENTREPRISE
echo "$affiliation" | cut -d "|" -f4 # TA_MAIL_ENTREPRISE
echo "$affiliation" | cut -d "|" -f5 # TA_FORM_CONTACT_ENTREPRISE
echo "$affiliation" | cut -d "|" -f6 # TA_TEL_ENTREPRISE
echo "$affiliation" | cut -d "|" -f7 # TA_NOM_GERANT
echo "$affiliation" | cut -d "|" -f8 # TA_PRENOM_GERANT
echo "$affiliation" | cut -d "|" -f9 # TA_FONCTION_GERANT
echo "$affiliation" | cut -d "|" -f10 # TA_TEL_GERANT
echo "$affiliation" | cut -d "|" -f11 # TA_MAIL_GERANT
echo "$affiliation" | cut -d "|" -f12 # TA_NOTES
# Affecter les variables.
TA_NOM_SITE=$(echo "$affiliation" | cut -d '|' -f1)
echo "TESTER VARIABLE $TA_NOM_SITE";
TA_NOM_ENTREPRISE=$(echo "$affiliation" | cut -d '|' -f2)
echo "TESTER VARIABLE $TA_NOM_ENTREPRISE";
...

Afficher une image

#!/bin/bash
zenity --text-info --title="Forums" --width=1000 --height=800  --html --url="http://i.imgur.com/7izQZgD.jpg" --checkbox="I'm sure I want to remove Windows"
if [ "$?" -eq "0" ];then
      zenity --info --text="You clicked OK,\nWindows destroyed. :)"
else
       exit 0
fi

MySQL avec Bash et Zenity

Connexion à une base MySQL : mysql -uUTILISATEUR -pPASSWORD -hSERVEUR -D BASEDEDONNEES -e 'SELECT * FROM TABLE'
Connexion SSL à ajouter.
Intégrer les variables récupérées avec Zenity dans une requête SQL.

Informer d'une erreur

zenity --error --title="An error Occurred" --text="A problem occurred while running the shell script."

Ajouter de la couleur dans Zenity

Utiliser les balises span.
<span color=\"green\">green</span>

Changer la taille du texte dans Zenity

# Changer la couleur, la taille, retour à la ligne.
zenity --info --text 'Some\nbig text\n\n(it is also blue)'

Créer une fenêtre Zenity avec l'assistant graphique Zenitor_3

apt install python-glade2 python-vte zenity python-gtksourceview2
apt update
Source : https://doc.ubuntu-fr.org/zenitor_3
Voir les dernières versions : https://launchpad.net/~hizo/+archive/ubuntu/zenitor
Télécharger le paquet : https://launchpad.net/~hizo/+archive/ubuntu/zenitor/+files/zenitor_3.6.0~ppa1~raring_all.deb
dpkg -i zenitor_3.6.0~ppa1~raring_all.deb
Lancer Zenitor 3 depuis le menu.

Yad

Un fork de Zenity prometteur : https://sourceforge.net/projects/yad-dialog/

WinZenity

WinZenity (Zenity Portable for Windows) : https://github.com/maravento/winzenity

Bibliographie

Source : https://doc.ubuntu-fr.org/zenity
Source : https://enacit1.epfl.ch/cours_matlab/zenity.shtml
Source : https://fr.scribd.com/document/48032243/Simple-Zenity-Guide
Source : https://www.linux.org/threads/zenity-gui-for-shell-scripts.9802/
Source : http://www.chicoree.fr/w/Afficher_des_bo%C3%AEtes_de_dialogue_avec_Zenity
Source : http://linux.byexamples.com/archives/265/a-complete-zenity-dialog-examples-2/
Source : https://www.howtogeek.com/107537/how-to-make-simple-graphical-shell-scripts-with-zenity-on-linux/
Source : http://inouire.net/blog/index.php?post/2009/05/24/zenity%3A-la-plus-simple-des-interfaces-graphiques

Utiliser des commandes Shell avec le terminal

Utiliser des commandes Shell avec le terminal est un pense bête pour administrer GNU/Linux.

Programmation en Shell Bash Linux

Programmation en Shell Bash Linux est un pense bête pour programmer en Shell Bash.

NAVIGATION

PARTICIPER ET PARTAGER

Bienvenue sur le wiki de Amis SH.
De nombreuses pages sont partagées sur ce wiki.
Créer un compte utilisateur pour participer sur le wiki.
Les pages présentées sur le wiki évoluent tous les jours.
Certaines recherches sont peu abouties et incluent des erreurs.
Utiliser la recherche interne du wiki pour trouver votre contenu.
La page de discussion de Amis SH vous permet de poser vos questions.
Consulter le site amis-sh.fr pour installer votre propre serveur web.
Améliorer le contenu des pages avec vos retours depuis l'onglet discussion.
Ce contenu ne doit pas servir à nuire à autrui ou à un système informatique.
Protéger votre système Linux ou Windows avec cette page dédiée à la sécurité.

SOUTENIR CE WIKI

Soutenir le wiki avec un don en monnaie numérique :
AEON - Bitcoins - Bitcoins Cash - Bitcoins Gold - Bitcore - Blackcoins - Basic Attention Token - Bytecoins - Clams - Dash - Monero - Dogecoins - Ğ1 - Ethereum - Ethereum Classique - Litecoins - Potcoins - Solarcoins - Zcash

OBTENIR DE LA MONNAIE NUMERIQUE

Obtenir gratuitement de la monnaie numérique :
Miner de la cryptomonnaie.