tutorials:bash_scripting:part2
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tutorials:bash_scripting:part2 [2012/02/26 01:31] – rmiles | tutorials:bash_scripting:part2 [2017/10/12 10:58] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | //**3. Adding a GUI interface to a bash script**// | + | //**3. Adding a GUI interface to a bash script**//\\ |
| - | GUI interfaces can be used with bash scripts. The script below uses //Zenity// to provide a GUI interface | + | ---- |
| + | GUI interfaces can be used with bash scripts. The script below uses //Zenity// to provide a GUI dialog | ||
| <code bash> | <code bash> | ||
| Line 16: | Line 17: | ||
| ############################################################ | ############################################################ | ||
| - | input_file=$(zenity --file-selection --title " | + | |
| + | input_file=`zenity --file-selection --title=" | ||
| if [ $? -eq 1 ]; then | if [ $? -eq 1 ]; then | ||
| - | | + | exit |
| - | elif [ $? -eq 1 ]; then | + | fi |
| - | zenity --error --text "You did not select a file to convert" | + | |
| - | exit | + | |
| - | fi | + | |
| working_dir=$(dirname $input_file) | working_dir=$(dirname $input_file) | ||
| Line 29: | Line 28: | ||
| cd $working_dir | cd $working_dir | ||
| - | output_file=$(zenity --file-selection --save --title "Where do you want to save the pdf file?"\ | + | output_file=`zenity --file-selection --save --title="Save .pdf file as..." --confirm-overwrite` |
| - | --directory $working_dir | + | |
| + | if [ $? -eq 1 ]; then | ||
| + | exit | ||
| + | fi | ||
| + | |||
| convert $input_file $output_file | convert $input_file $output_file | ||
| + | |||
| + | zenity --question --text=" | ||
| + | |||
| + | case $? in | ||
| + | 0) rm $input_file;; | ||
| + | |||
| + | 1) exit;; | ||
| + | esac | ||
| # end of script | # end of script | ||
| Line 43: | Line 53: | ||
| </ | </ | ||
| - | The first line of this script uses a zenity command within two backticks to assign a file to the variable // | + | The first line of this script uses a zenity command within two backticks to assign a file to the variable // |
| + | |||
| + | The script | ||
| + | |||
| + | <code bash> | ||
| + | if [ $? -eq 1 ]; then | ||
| + | exit | ||
| + | fi | ||
| + | </ | ||
| + | |||
| + | If a file has been selected and the user has pressed **OK** the script will continue on to the next line after the if statement This line creates the variable // | ||
| + | |||
| + | <code bash> | ||
| + | working_dir=$(dirname $input_file) | ||
| + | |||
| + | cd $working_dir | ||
| + | </ | ||
| + | |||
| + | Next the script creates a variable, // | ||
| + | |||
| + | Another if statement is required in case the user hits the *Cancel* button. | ||
| + | |||
| + | <code bash> | ||
| + | output_file=`zenity --file-selection --save --title=" | ||
| + | |||
| + | if [ $? -eq 1 ]; then | ||
| + | exit | ||
| + | fi | ||
| + | </ | ||
| + | |||
| + | The convert command is exactly the same as in the previous script. | ||
| + | |||
| + | <code bash> | ||
| + | convert $input_file $output_file | ||
| + | </ | ||
| + | |||
| + | However, in this script provides the option of deleting the image file after the .pdf file has been created. | ||
| + | |||
| + | <code bash> | ||
| + | zenity --question --text=" | ||
| + | |||
| + | case $? in | ||
| + | 0) rm $input_file;; | ||
| + | |||
| + | 1) exit;; | ||
| + | esac | ||
| + | </ | ||
| + | |||
| + | The //zenity --guestion// | ||
| + | |||
| + | A case statement follows which evaluates | ||
| - | The CLI utility con2pdf either converted an image to a pdf if an image file was entered after the command or it printed out the usage message | + | ---- |
| + | //There are other GUI dialog programs. KDE has //kdialog// which is based on the Qt toolkit, there is also //dialog//, // | ||
| - | Zenity returns | + | In the next section I'' |
| - | 0 The user has pressed either **OK** or **Close**. | + | ---- |
| - | | + | **Cheers!** |
| - | -1 An unexpected error has occurred, e.g. no file has been selected. | + | |
| - | 5 The dialog has been closed because the timeout has been reached. | + | |
| - | An if/else if (elif) statement can be used to exit the script with a //0// exit code or will open a zenity error dialog and then exit the script if the user presses **Cancel** or does not select a file to convert. | ||
tutorials/bash_scripting/part2.1330219862.txt.gz · Last modified: 2017/10/12 10:58 (external edit)