How to use the command 'zenity' (with examples)
- Linux
- December 17, 2024
Zenity is a versatile command-line utility that allows the display of graphical dialog boxes from scripts or directly from the shell prompt. This is particularly useful for developers and system administrators who need to create or automate user interactions within their scripts but want to offer a graphical interface. Zenity can handle various types of dialogs, including messages, forms, file selection, and progress dialogs, returning user input or an error code.
Use case 1: Display the default question dialog
Code:
zenity --question
Motivation:
There are times when a script needs user confirmation to proceed with an action, like making changes to system settings or files. A question dialog is a straightforward method to obtain a simple Yes or No answer from the user, making it a fundamental part of interactive scripts.
Explanation:
--question
: This argument is used to create a dialog box with a question mark, prompting the user to answer Yes or No. By default, it returns exit code 0 for Yes and 1 for No, which can be used to control the flow in a script.
Example Output:
A dialog box appears with a question mark icon, two buttons labeled “Yes” and “No,” and a message asking the user to confirm an action. Upon clicking, the response is returned as an exit code.
Use case 2: Display an info dialog displaying the text “Hello!”
Code:
zenity --info --text="Hello!"
Motivation:
Informational dialogs are useful for conveying important messages to users. You might want to notify the user of the completion of a task, provide a simple greeting, or offer guidance without expecting any input, other than acknowledgment.
Explanation:
--info
: Specifies that the dialog should be an informational one, usually with an “i” icon to denote information.--text="Hello!"
: This sets the text displayed in the dialog box. It customizes the message for the user to read.
Example Output:
A small dialog box appears with an information symbol and the word “Hello!” in the center, along with an “OK” button to acknowledge and close the message.
Use case 3: Display a name/password form and output the data separated by “;”
Code:
zenity --forms --add-entry="Name" --add-password="Password" --separator=";"
Motivation:
Forms are perfect for gathering multiple pieces of information from a user in a single step. If a script requires user credentials or contact information, a form dialog can collect these inputs efficiently while maintaining a user-friendly appearance.
Explanation:
--forms
: Creates a form dialog box that can include multiple input fields.--add-entry="Name"
: Adds an input field labeled “Name” for text entry.--add-password="Password"
: Adds a password entry field where user input is masked for security.--separator=";"
: Sets the character used to separate multiple input values in the output, allowing their easy parsing in scripts.
Example Output:
A dialog box presents two fields, “Name” and “Password.” After the user fills these and submits, the inputs are returned in a single line separated by a semicolon, such as “John Doe;supersecretpassword”.
Use case 4: Display a file selection form in which the user can only select directories
Code:
zenity --file-selection --directory
Motivation:
For scripts that perform operations on directories rather than individual files, it is crucial to ensure the user selects a directory. This prevents errors related to improper file paths and streamlines the user’s workflow by narrowing their choices.
Explanation:
--file-selection
: Opens a dialog for selecting files or directories.--directory
: Restricts the selection to directories only, preventing files from being chosen.
Example Output:
A file chooser window appears, allowing the user to browse folders. The user’s directory selection is returned as a path, facilitating further script operations on that particular location.
Use case 5: Display a progress bar which updates its message every second and show a progress percent
Code:
(echo "#1"; sleep 1; echo "50"; echo "#2"; sleep 1; echo "100") | zenity --progress
Motivation:
Progress bars are invaluable for providing visual feedback during long-running operations. They assure users that a task is advancing and help estimate time to completion, enhancing user experience, especially in performance-intensive scripts.
Explanation:
--progress
: Creates a dialog box with a progress bar.(echo "#1"; sleep 1; echo "50"; echo "#2"; sleep 1; echo "100")
: This command sequence simulates a task updating progress at intervals. It outputs messages and percentages to update the progress bar:echo "#1"
displays a message in the progress dialog.sleep 1
pauses execution for a second, simulating work.echo "50"
andecho "100"
update the progress percentage.
Example Output:
A small dialog box appears with a moving progress bar. Initially, the bar advances to 50% with a message “#1”, then to 100% with a message “#2”, all within the span of two seconds.
Conclusion:
Zenity’s flexibility in displaying graphical dialogs from the command line makes it a powerful tool for script writers and developers. Whether you need to confirm user actions, inform them of progress, or capture user input, Zenity’s simple commands can transform text-based interactions into a more engaging and intuitive experience. By leveraging Zenity, you can greatly enhance the usability of your scripts.