User Tools

Site Tools


tutorials:bash_scripting:part2

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorials:bash_scripting:part2 [2012/02/26 16:15] rmilestutorials:bash_scripting:part2 [2017/10/12 21: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 dialog for selecting an image to convert to .pdf, a second GUI dialog for naming and saving the newly created .pdf file and a third offering the option to delete the image file used to create the .pdf file.  GUI interfaces can be used with bash scripts. The script below uses //Zenity// to provide a GUI dialog for selecting an image to convert to .pdf, a second GUI dialog for naming and saving the newly created .pdf file and a third offering the option to delete the image file used to create the .pdf file. 
  
Line 54: Line 55:
 The first line of this script uses a zenity command within two backticks to assign a file to the variable //input_file//. The option //file-selection// specifies that a file selection dialog box is required. The option //--title// will create a title for the dialog using the text enclosed in double quotes. If the user is running this script from an icon on the desktop, The file selection dialog will open into /home/[user-directory]. The first line of this script uses a zenity command within two backticks to assign a file to the variable //input_file//. The option //file-selection// specifies that a file selection dialog box is required. The option //--title// will create a title for the dialog using the text enclosed in double quotes. If the user is running this script from an icon on the desktop, The file selection dialog will open into /home/[user-directory].
  
-The script //con2pdf// in Section 1 either converted an image to a .pdf if an image file was entered after the command //con2pdf// or it printed out the usage message and then returned to the command prompt. However, in the //zenity// file selection dialog there are to user options, select a file or **Cancel**. It is necessary after this zenity command executes to check the exit code to see if the user has hit *Cancel* in which case the exit code would be 1. The following if statement should look familiar.+The script //con2pdf// in Section 1 either converted an image to a .pdf if a target image file was entered after the command //con2pdf// or it printed out the usage message and then returned to the command prompt if no target file was entered. However, in the //zenity// file selection dialog there are two user options, select a file or **Cancel**. It is necessary after this zenity command executes to check the exit code to see if the user has hit *Cancel* in which case the exit code would be 1. The following if statement should look familiar.
  
 <code bash> <code bash>
Line 62: Line 63:
 </code> </code>
  
-If a file has been selected and the user has pressed **OK** the script will continue on to the next line after the if/elif statement which creates the variable //working_dir// and assigns it the path of //$input_file// using the command //dirname//. This variable is then used to cd into the target file directory and that directory now becomes the script's //pwd?// and the next //zenity// command will open a file selection dialog in that directory.+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 //working_dir// and assigns it the path of //$input_file// using the command //dirname//. This variable is then used to cd into the target file'directory. That directory now becomes the script's //pwd// and the next //zenity// command will open a file selection dialog in that directory.
  
 <code bash> <code bash>
Line 70: Line 71:
 </code> </code>
  
-Next the script creates a variable, //output_file//. This //zenity// command is also a file selection dialog. However, the //--save// option is used even though the script is creating a variable and not saving a file so that the //--confirm-overwrite// option can be used. This is necessary because the command //convert// will overwrite files without warning.+Next the script creates a variable, //output_file//. This //zenity// command is also a file selection dialog. The //--save// option is used even though the script is creating a variable and not saving a file so that the //--confirm-overwrite// option can be used. This is necessary because the command //convert// will overwrite files without warning.
  
 Another if statement is required in case the user hits the *Cancel* button. Another if statement is required in case the user hits the *Cancel* button.
Line 88: Line 89:
 </code> </code>
  
-However, in this script I provide the option of deleting the image file after the .pdf file has been created.+However, in this script provides the option of deleting the image file after the .pdf file has been created.
  
 <code bash> <code bash>
Line 100: Line 101:
 </code> </code>
  
-The //zenity --guestion// dialog prints the text into a pop up window and provides two buttons, **Yes** and **No**. //Zenity// returns //0// if yes and //1// if no.+The //zenity --guestion// dialog prints the text into a pop up window that provides two buttons, **Yes** and **No**. //Zenity// returns //0// if yes and //1// if no.
  
 A case statement follows which evaluates the exit code and will either delete the file or exit the script accordingly. Case statements are handy for this type of task and are generally used instead of if statements when multiple options may be involved. Note the syntax as well as that a case statement must close with //esac//. A case statement follows which evaluates the exit code and will either delete the file or exit the script accordingly. Case statements are handy for this type of task and are generally used instead of if statements when multiple options may be involved. Note the syntax as well as that a case statement must close with //esac//.
  
 ---- ----
-There are other GUI dialog programs. KDE has //kdialog// which is based on the Qt toolkit, there is also //dialog//, //xdialog//, //yad/ and others (I'm sure). Their features and syntax vary but if you understand the basics of how //zenity// was used in this script you shouldn't have any trouble sorting out how to use one of the others. +//There are other GUI dialog programs. KDE has //kdialog// which is based on the Qt toolkit, there is also //dialog//, //xdialog//, //yad/ and others (I'm sure). Their features and syntax vary but if you understand the basics of how //zenity// was used in this script you shouldn't have any trouble sorting out how to use one of the others//.
- +
-In the next section I''ll discuss a script I use on our print server to shut it down if there is nobody using the lan. +
  
 +In the next section I''ll discuss a script I use on our print server to shut it down if there is nobody using the lan.//
 +----
 +**Cheers!**
  
tutorials/bash_scripting/part2.1330233341.txt.gz · Last modified: 2017/10/12 21:58 (external edit)