Shell Scripting Interfaces with Dialog and Zenity Ray








![Illustration – Input variables #!/bin/bash # file: input_variables. sh if [ $1 ] ; Illustration – Input variables #!/bin/bash # file: input_variables. sh if [ $1 ] ;](https://slidetodoc.com/presentation_image/17b02afe5ca9c7c7ab0ebc5cc7f0b0a9/image-9.jpg)































- Slides: 40
Shell Scripting Interfaces with Dialog and Zenity Ray Smith Portland General Electric Session #716
Speaker Qualifications • Database administrator at PGE • Shell scripting for 10 years – Three years of scripting on Linux • Built interactive schema cloner for ISP – On-demand exp/imp – Required database authentication – Cross-instance copies
The Cloner • Multiple user inputs required – Username, password, source and destination SIDs – Reuse earlier export dmp file – Verify all input values prior to clone run-time • Had to verify several input values – Okay to copy from SIDa to SIDb – User account exists on both SIDs • Suggest target schema names – Accept user's edits
Cloner History • Inherited PL/SQL packaged procedure – Security issues: shared user name and password – Needed to keep production data out of development • Went to command-line with 'read' • Upgraded to 'dialog' – Tremendous success • Upgraded to 'zenity' – Deployment disaster
Agenda • Background and requirements • Common elements • Basic capabilities – Dialog – Zenity • Demonstrations
Calibration • Experience with dialog or zenity • Anticipated usefulness of this presentation
Run-time input options • Command-line variables • Edit the script • Use a configuration file • Prompt for values with 'read' • Programming with 'dialog‘ or ‘zenity’
Command-line variables • Require the user to specify a value – Remember to fail gracefully • Positional parameters – File name is $0 – First unnamed variable is $1 – Second unnamed variable is $2, etc • Works great in crontab – Scales easily
Illustration – Input variables #!/bin/bash # file: input_variables. sh if [ $1 ] ; then echo "Amaze your $1" else echo “provide input, pathetic human!" fi > >. /input_variables. sh friends > Amaze your friends
Upside and downside • Easy to deploy without customization – No localization required • Securable – Does not require write permissions for others • Gets out-of-hand with too many variables >. /many_input_variables. sh one two three four five six seven eight nine ten eleven
Edit the script • Edit static values within the script – Segregate variables section within your script • Upside and downside – Static script is Very Predictable – Simpler cron commands – High maintenance cost • Customize with each deployment • Update individual copies with each release of your script
Read command • Prompt for user input at the command-line • Upside and downside – Quick and flexible – Portable across operating systems • Be aware of Korn shell syntax – Worthless for cron – Depends on the user to read the prompt • Bigger hurdle than it appears
Illustration – read command #!/bin/bash # file: read_demo. sh read -p "Whom would you like to impress: " my. Val if [ $my. Val ] ; then echo “Impress my ${my. Val}" else echo “Too tired to type right now? " fi >. /read_demo. sh > Whom would you like to impress: > Impress my secretary
Dialog overview Man: Dialog is a program that will let you to present a variety of questions or display messages using dialog boxes from a shell script • Dialog writes output to standard error (2>) – Capture these values for use by your script • Arguments are preceded by two hyphens – Dash dash arg
Dialog boxes • All dialog boxes require: – Type • Includes caption, purpose, or contents of the box – Height • Height of the dialog box – Width • Width of the dialog box • Sizing values do not require call-out – Just state the values after everthing else
Minimal dialog syntax dialog –-inputbox=“Who are you? ” 5 50 >mytemp. lst echo “`cat mytemp. lst` is the user” dialog export -–inputbox=“Enter username” 5 80 2>$my. USER –-inputbox=“Enter password” 5 80 2>$my. PASS –-inputbox=“Which database? ” 5 80 2>$my. SID my. Connect=${my. USER}/${my. PASS}@${my. SID} sqlplus –s ${my. Connect} <<EOF SELECT sysdate FROM dual; exit EOF
Dialog return codes
Applying Return Codes dialog --inputbox "Who are you? " 5 50 2>tempfile. lst export my. Answer=`cat tempfile. lst` dialog --yesno “Are you really ${my. Answer}? “ 5 50 retval=$? case $retval in 0) dialog --infobox “I knew it was you“ 5 50; ; 1) dialog --infobox “Show some ID“ 5 50; ; *) echo “Question must be too hard”; ; esac
Six demonstrations • What a brave presenter!
Zenity Syntax • Cleaner than dialog – Box size not required – In-line processing – Same ‘dash-dash arg’ syntax • Iconized messages boxes – – Error Info Question Warning
Same Program in Zenity export my. Name=`zenity --entry --text "Who are you? "` zenity --question --text "Are you really $my. Name ? " case $? in 0) zenity --info --text "I knew it was you"; ; 1) zenity --error --text "Show some ID, please"; ; *) echo "Unexpected error has occurred"; ; esac
Zenity return codes • Zenity does not have a Yes button • No programmable Help key either
Password Forms • Password forms – Dialog has a specific form (passwordbox) – Zenity allows you the option of hiding text values • Trade user concerns for security – Dialog’s ‘insecure’ option – Zenity entry box with ‘hide-text’
Dialog Password Syntax dialog –-passwordbox "Enter your password: " 15 35 2>temptxt case $? in 0) echo "Your password is `cat temptxt`" ; ; 1) echo "No answer was given" ; ; 255) exit 0 ; ; esac dialog –-insecure –-passwordbox "Enter your password: " 15 35 2>temptxt case $? in 0) echo "Your password is `cat temptxt`" ; ; 1) echo "No answer was given" ; ; 255) exit 0 ; ; esac
Zenity Password syntax export my. Val=`zenity --entry --text "Enter your password " --entry-text "someseedvalue" --hide-text` case $? in 0) echo "Your password is ${my. Val}" ; ; 1) echo "No answer was given"; ; -1) exit 0; ; esac
List Boxes in Dialog • Third size parameter required for row size • Two columns – The selection value – A description or other text
Dialog List Box Syntax dialog --menu "Select the database instance" 17 80 6 "NICKEL" "Five Cent Database" "URANIUM" "Not-For-Export Database" "CUSTOM" "User defined instance" 2> tempfile. lst retval=$? case $retval in 0) my. SOURCESID=`cat tempfile. lst`; ; 1) exit 0 ; ; 255) exit ; ; esac case `echo $my. SOURCESID` in NICKEL) echo "Logging into NICKEL. . . "; ; URANIUM) echo "Logging into URANIUM. . . "; ; CUSTOM) read -p "Enter the SID you want: "; ; *) echo "Give me something to work with!"; ; esac
Same Thing in Zenity my. SID=`zenity --list --text "Select the database instance" --column "SID" --column "Description" "NICKEL" "Five Cent Database" "URANIUM" "Not-For-Export Database" "CUSTOM" "User defined instance" ` case `echo $my. SID` in NICKEL) echo "Logging into NICKEL. . . "; ; URANIUM) echo "Logging into URANIUM. . . "; ; CUSTOM) read -p "Enter the SID you want: "; ; *) echo "Give me something to work with!"; ; esac • Explicit declaration of column names – Become column headings
Odds-n-Ends: Using Titles • Add clarity to the GUI with titles – Helpful for resolving user problems, too • Identical syntax in both applications dialog –-title “Identify Yourself” --inputbox "Who are you? " 5 50 2>tempfile. lst zenity –title “Identify Yourself” --entry –-text “Who are you? ”
Odds-n-Ends: Clearing and widgets • Use helper options for dialog – – – ‘Clear’ function removes earlier dialogs Use ‘and-widget’ to chain series of screens ‘Timeout’ exits after a fixed duration ‘Backtitle’ for window header ‘Default-item’ highlights the default in a list • Set recurring values (app title, f. e. ) as variables
Dialog options syntax dialog --backtitle "${BACK_TITLE}" --clear --default-item ${my. SOURCESID} --title "Database Selection" --timeout 90 --menu "Select database” 17 80 10 . . .
Other dialog screens
Other dialog screens
Integrated Demonstration • Gathering a connect string
Requirements • Only available on Linux sudo apt-get install dialog sudo apt-get install zenity • User terminals must be compatable – Cygwin or pu. TTY for either – Xserver required for zenity • You must invest the time in error handling
Selection • Dialog has much more to offer – Options – Configuration – Simplicity • Zenity is more attractive – Pop-up boxes • Zenity requires xserver
Enough information for one hour? • This presentation demonstrated key concepts – Building dialog boxes – Using values received from the user – Chaining boxes together to build an application • Man pages are fairly good for dialog & zenity – Describes various box ‘types’ – Adequate information to build each type • White paper contains detailed stuff
Questions?
Additional Resources • Help and man pages dialog –help zenity --help • On-line resources http: //linux. byexamples. com/archives/259/a-complete-zenity-dialog-examples-1/ http: //linux. byexamples. com/archives/265/a-complete-zenity-dialog-examples-2/
Thank You • Please complete the evaluation form – Ray Smith – Shell Scripting Interfaces with Dialog and Zenity – Session #716 • bokonen@comcast. net