dialog - Ideal Penn Group

Download Report

Transcript dialog - Ideal Penn Group

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
• Why not use 3GL languages like perl?
– Transparency
– Portability
– Maintenance
Run-time input options
•
Command-line variables
•
Edit the script
•
Use a configuration file
•
Prompt for values with 'read'
•
Programming with 'dialog‘ or ‘zenity’
Option 1: Command-line variables
• Require the user to specify a value
– Remember to fail gracefully
$0
$1
$2
• Positional parameters
run_me.sh otterface
– File name is $0
– First unnamed variable is $1
– Second unnamed variable is $2, etc
• Works great in crontab
– Scales easily
orcl01
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
Option 2: 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: " myVal
if [ $myVal ] ; then
echo “Impress my ${myVal}"
else
echo “Too tired to type right now?"
fi
> ./read_demo.sh
> Whom would you like to impress:
> Impress my secretary
secretary
Option 3: Dialog
“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?” 0 0 >mytemp.lst
echo “`cat mytemp.lst` is the user”
dialog
dialog
dialog
export
-–inputbox=“Enter username” 0 0 2>$myUSER
–-inputbox=“Enter password” 0 0 2>$myPASS
–-inputbox=“Which database?” 0 0 2>$mySID
myConnect=${myUSER}/${myPASS}@${mySID}
sqlplus –s ${myConnect} <<EOF
SELECT sysdate FROM dual;
exit
EOF
Demonstration
inputbox_d1.sh
inputbox_d2.sh
Dialog return codes
0
1
If dialog is exited by pressing the Yes or OK
button
If the No or Cancel button is pressed
2
If the Help button is pressed
3
If the Extra button is pressed
-1
If errors occur inside dialog or dialog is exited
by pressing the ESC key
Applying Return Codes
dialog --inputbox "Who are you?" 5 50 2>tempfile.lst
export myAnswer=`cat tempfile.lst`
dialog --yesno “Are you really ${myAnswer}?“ 5 50
retval=$?
case $retval in
0) dialog --infobox “I knew it was you“ 5 50;;
1) dialog --infobox “Show some ID“ 5 50;;
*) exit 0;;
esac
Demonstration
returncodes_d.sh
returncodes_z.sh
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 myName=`zenity --entry --text "Who are you?"`
zenity --question --text "Are you really $myName ?"
case $? in
0) zenity --info --text "I knew it was you";;
1) zenity --error --text "Show some ID, please";;
*) exit 0;;
esac
Demonstration
inputbox_z.sh
Zenity return codes
0
1
-1
The user has pressed either OK or Close
button
The user has either pressed Cancel or used
window functions to close the dialog
An unexpected error has occurred
• 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 ;;
Demonstration
passwords_d1.sh
esac
passwords_d2.sh
Zenity Password syntax
export myVal=`zenity
--entry \
--text "Enter your password " \
--entry-text "someseedvalue" \
--hide-text`
case $? in
0) echo "Your password is ${myVal}" ;;
1) echo "No answer was given";;
-1) exit 0;;
esac
Demonstration
passwords_z.sh
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) mySOURCESID=`cat tempfile.lst`;;
1) exit 0 ;;
255) exit ;;
esac
case `echo $mySOURCESID` in
NICKEL) echo "Logging into NICKEL . . .";;
URANIUM) echo "Logging into URANIUM . . .";;
CUSTOM) read -p "Enter the SID you want: ";;
*) exit 0;;
esac
Demonstration
listmenu_d.sh
Same Thing in Zenity
mySID=`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 $mySID` in
NICKEL) echo "Logging into NICKEL . . .";;
URANIUM) echo "Logging into URANIUM . . .";;
CUSTOM) read -p "Enter the SID you want: ";;
*) exit 0;;
esac
• Explicit declaration of column names
– Become column headings
Demonstration
listmenu_z.sh
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
timeout x exits after x seconds
backtitle for window header
title for a title in the dialog box border
default-item highlights the default in a list
yes-label and no-label alternate button text
• Set recurring values (app title, f.e.) as
variables
Dialog options syntax
dialog --backtitle "${BACK_TITLE}" \
--clear \
--default-item ${mySOURCESID} \
--title "Database Selection" \
--timeout 90 \
--menu "Select database” \
17 80 10 \
. . .
Demonstration
betterflow_d.sh
Other dialog screens
Option Type
checklist
menu
radiolist
Example
dialog --checklist "Pick all you like" 0 0 0 \
"1)" "One Selection" "on" \
"2)" "Another Guess" "off"
dialog --menu "Please select something" 0 0 0 \
"A)" "Make popcorn" \
"B)" "Open a cool one" \
"C)" "Go back to work"
dialog --radiolist "Pick any one" 0 0 0 \
"1)" "One Selection" "on" \
"2)" "Another Guess" "off"
Other dialog screens
Option Type
dselect
fselect
editbox
tailbox
dialog
dialog
dialog
dialog
Example
--dselect /etc 0 0
--fselect /etc 0 0
–editbox hello.txt 0 0
–tailbox hello.txt 0 0
Dialogrc Files
• Use .dialogrc files to optimize visual impact
– Stored in your home directory
– Can be deployed through your script
• Consist of curses standard colors
– Red,blue,black,white,green,yellow,magenta
– White is grey
• Play with alternatives you find on the web
– In the whitepaper, too
Demonstration
use_rays_rc.sh
use_commander_rc.sh
Requirements
• Only available on Linux
sudo apt-get install dialog
sudo apt-get install zenity
• User terminals must be compatable
– Cygwin or puTTY 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
• Good man pages for dialog & zenity
– Describes various box ‘types’
– Adequate information to build each type
– Both also have useful –help screens
• White paper contains details and all demo scripts
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
• [email protected]