Every system administrator needs to use shell scripts to automatically execute a lot of daily work from time to time. In fact, many of these little helpers come from the command line and sometimes need to be input by users. You can use a mature Python programming language or Zenity tool.
Zenity is a GTK +-based dialog box tool. It provides the Administrator with many useful command line windows for user input and output, which can be easily applied to shell scripts. Although its tool list is relatively limited, it still provides the date selector, file/directory selection, list box, message box and progress bar. Zenity is not a direct script, but a tool that provides standard output results in your script. In this Zenity tutorial, We have browsed a lot of practice instances on how to use Zenity to make the previously exposed backup more secure.
Zenity entry
Quick Google search will find links to many Zenity tutorials and Zenity Manual official website. You can get a feeling of using Zenity to do something. The best way is to open the terminal and rotate it. Zenity has built-in help options. It provides a list of all options, including help for each dialog box. The Main Dialog Box includes calendar, input, error, message, file selection, list, notification, progress, problem, warning, proportion, and text information.
Many dialogs have some simple options, such as text input dialog box. Options include-go-default text, text displayed on the input box; -- hide-hide your input when you enter the password. If you enter a simple command on the terminal, you will see the dialog box and the result will be returned to the screen. For example, use the-access command as follows:
$ zenity --entry --text=”Please enter your name”
Sometimes you may take different actions based on different user input, such as clicking the cancel key. This type of interaction requires some shell scripts to capture the output of the dialog box, but this is not complicated. If you are not familiar with shell or Bash scripts, you can find some useful tutorials on Bash programming on the Internet. If you prefer paper books, you can try the latest Unix and Linux system management manuals. It has a whole chapter about the script, and the other 31 chapters are also related to shell, which needs to be mastered as a Linux system administrator.
Files And Directories
One of the most common tasks in a script is to use the tasks being executed to select a desired file or directory. The Zenity file selection dialog box provides the access standard GTK + file dialog box and outputs all paths of selected files. This dialog box contains many options: -- OK-overwrite associated files, -- save prompt-if you select to use an existing file, the result will overwrite the current file.
The following script displays the result stored in a file selection dialog box and the strFileName variable:
$ strFileName=$(zenity --file-selection --save --confirm-overwrite); echo $strFileName
'$' Followed by '=' prompts Bash to replace the Zenity command output as a target for assigning values to the strFileName variable. Replace the parameter variable strFileName of the echo command with the character '$'. The result is returned to the terminal screen.
Other options in the file selection dialog box include: -- multiple is used to select multiple files; -- file is used to set what the file filter will display; -- directory is used to activate the directory read-only mode. Before the select file dialog box is displayed, it is easy to understand how the text input dialog box is associated with the select filter mode. When you use the -- multiple option, you can also use the -- separator option to set what characters will be used to separate file names. If this parameter is not set, '|' is used by default '.
Summary
Through a small experiment, you should be able to master how to add the Zenity dialog box to an existing or new script. Any script that requires user input will be a good object and the output should be displayed in a standard box. -- List option to complete a great solution, if you need to display the information list from the user's choice. There are many options like the file selection dialog box, such as -- checklist and -- radiolist used to set the first column of the check box or single choice button.
The following is a simple single-line file tool to search for. log files and display them in The ListBox dialog box:
x=$(find / -name “*.log” |zenity --list --column “Delete” --column “Files” --checklist --height 600 --width 600 --separator=”,”); echo $x
Zenity is worth the effort to learn how to use it effectively. Once you start using it, you will not regret it.