Programmation Shell script GUI
Sommaire
Les interfaces graphiques.......................................................................................3
Présentation.................................................................................................................... 3
La commande dialog....................................................................................................... 5
La commande Xdialog..................................................................................................... 6
Identifier le terminal........................................................................................................ 7
Récupérer des données................................................................................................... 8
Checklist et Textbox..............................................................................................11
Logbox..................................................................................................................12
Un kill graphique..................................................................................................13
Annexe 1...............................................................................................................15
1 Cours
[Link] ♦ 11/09/2015 rev.8
Copyright 2010-2015 tv <tvaira@[Link]>
Permission is granted to copy, distribute and/or modify this document under the terms of the
GNU Free Documentation License,
Version 1.1 or any later version published by the Free Software Foundation; with no Invariant
Sections, with no Front-Cover Texts, and with no Back-Cover.
You can obtain a copy of the GNU General Public License : write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2
Programmation Shell script GUI Les interfaces graphiques
Les interfaces graphiques
Présentation
Il est possible d'ajouter une IHM graphique (à base de boîtes de dialogue) pour les
shell scripts en utilisant un "dialog program" comme :
• le package gtkdialogs fournit gchooser,
filechooser, gmessage et xtest
$ gmessage -buttons
"Continuer:1,Arreter:0"
-center -print "Test"
• la commande gdialog (souvent remplacer par
zenity)
• la commande zenity
$ zenity --question --title "Alert"
--text "Windows a été détecté.
Voulez-vous le supprimer ?"
$ find . -name '*.sh' | zenity --title "Search Results"
--list --text="Finding all scripts ..." --column="Files"
• la commande kdialog
$ kdialog --combobox "Choisir l'OS"
Linux Windows
• les commandes dialog (mode console) et Xdialog (mode Xwindow).
On utilisera dialog et Xdialog.
3 Cours
Programmation Shell script GUI Les interfaces graphiques
La commande dialog
L'historique de cette commande se révèle assez compliqué puisque son auteur ayant
cessé de la supporter, plusieurs versions ont vu le jour.
Plus de détails sur : [Link]/dialog/
Avec la distribution Mandriva, vous trouverez le paquetage cdialog qui correspond à
une version de dialog. Pour vérifier sa présence, il suffit de taper dialog pour voir
apparaître une aide succinte. Sinon, installer le paquetage :
# urpmi cdialog
Pour obtenir l'aide complète :
$ man dialog
Des exemples et la documentation sont disponibles dans :
$ ls /usr/share/doc/cdialog-*
Quelques boîtes de dialogues disponibles :
--calendar <text> <height> <width> <day> <month> <year>
--checklist <text> <height> <width> <list height> <tag1>
<item1> <status1>...
--fselect <filepath> <directory> <height> <width>
--gauge <text> <height> <width> [<percent>]
--infobox <text> <height> <width>
--inputbox <text> <height> <width> [<init>]
--menu <text> <height> <width> <menu height> <tag1>
<item1>...
--msgbox <text> <height> <width>
--passwordbox <text> <height> <width> [<init>]
--radiolist <text> <height> <width> <list height> <tag1>
<item1> <status1>...
--tailbox <file> <height> <width>
--tailboxbg <file> <height> <width>
--textbox <file> <height> <width>
--timebox <text> <height> <width> <hour> <minute>
<second>
--yesno <text> <height> <width>
Exemple :
$ dialog --title "Titre" --msgbox "Hello world" 0 0
5 Cours
[Link] ♦ 11/09/2015 rev.8
La commande Xdialog
Plus de détails sur : [Link]. Pour vérifier sa présence, il suffit de taper
Xdialog pour voir apparaître une aide succinte. Sinon, installer le paquetage :
# urpmi Xdialog
Pour obtenir l'aide complète :
$ man Xdialog
Des exemples et la documentation sont disponibles dans :
$ ls/usr/share/doc/Xdialog-*
Quelques boîtes de dialogues disponibles :
--yesno <text> <height> <width>
--msgbox <text> <height> <width>
--infobox <text> <height> <width> [<timeout>]
--gauge <text> <height> <width> [<percent>]
--progress <text> <height> <width> [<maxdots> [[-]<msglen>]]
--inputbox <text> <height> <width> [<init>]
--2inputsbox <text> <height> <width> <label1> <init1> <label2>
<init2>
--3inputsbox <text> <height> <width> <label1> <init1> <label2>
<init2> <label3> <init3>
--combobox <text> <height> <width> <item1> ... <itemN>
--rangebox <text> <height> <width> <min value> <max value>
[<default value>]
--2rangesbox <text> <height> <width> <label1> <min1> <max1>
<def1> <label2> <min2> <max2> <def2>
--3rangesbox <text> <height> <width> <label1> <min1> <max1>
<def1> .. <label3> <min3> <max3> <def3>
--spinbox <text> <height> <width> <min value> <max value>
<default value> <label>
--2spinsbox <text> <height> <width> <min1> <max1> <def1>
<label1> <min2> <max2> <def2> <label2>
--3spinsbox <text> <height> <width> <min1> <max1> <def1>
<label1> ... <min3> <max3> <def3> <label3>
--textbox <file> <height> <width>
--editbox <file> <height> <width>
--tailbox <file> <height> <width>
--logbox <file> <height> <width>
--menubox <text> <height> <width> <menu height> <tag1> <item1>
{<help1>}...
--checklist <text> <height> <width> <list height> <tag1>
<item1> <status1> {<help1>}...
--radiolist <text> <height> <width> <list height> <tag1>
<item1> <status1> {<help1>}...
Les interfaces graphiques 6
Programmation Shell script GUI Les interfaces graphiques
--buildlist <text> <height> <width> <list height> <tag1>
<item1> <status1> {<help1>}...
--treeview <text> <height> <width> <list height> <tag1>
<item1> <status1> <item_depth1> {<help1>} .
--fselect <file> <height> <width>
--dselect <directory> <height> <width>
--calendar <text> <height> <width> <day> <month> <year>
--timebox <text> <height> <width>
Exemple :
$ Xdialog --title "Titre" --msgbox "Hello world" 0 0
Identifier le terminal
Technique n°1 :
On peut tester la variable d'environnement $DISPLAY :
#!/bin/bash
if [ -n "$DISPLAY" ]
then
# en mode graphique, on utilise Xdialog !!!
DIALOG="Xdialog"
else
# en mode console, on dialog !!!
DIALOG="dialog"
fi
$ DIALOG --title "Titre" --msgbox "Hello world" 0 0
et vérifier que le programme de dialogue est présent :
#!/bin/bash
# Quelle IHM utilisée ? (version complète)
if [ -n "$DISPLAY" ]
then
# en mode graphique, on utilise Xdialog !!!
[ -z "$DIALOG" -a -x "`which Xdialog 2>&-`" ] &&
DIALOG="Xdialog"
else
# en mode console, on dialog !!!
[ -z "$DIALOG" -a -x "`which dialog 2>&-`" ] &&
DIALOG="dialog"
fi
7 Cours