In this blog, we are going to learn about the ZENITY command, some basic options, and writing a script to handle Linux users using Zenity.

What is Zenity?

Zenity is a program that will display GTK+ dialogs, and return the user's input. This allows you to present information, and ask for information from the user, from all manner of shell scripts.

The application lets you create Graphical dialog boxes in the command line and makes the interaction between user and shell very easy.

There are other alternatives, but nothing compares to the simplicity of Zenity, especially when you don’t need complex programming. Zenity, a tool you must have your hands on.

Read more at: https://www.commandlinux.com/man-page/man1/zenity.1.html

SYNOPSIS

zenity [options]

Some of the features of Zenity

  • FOSS (Free and Open Source Software): Free and open-source software is software that is both free software and open-source software where anyone is freely licensed to use, copy, study, and change the software in any way, and the source code is openly shared so that people are encouraged to voluntarily improve the design of the software.
  • Cross-Platform Application: allow different software systems — typically Windows, Mac, and Linux (Android is built on top of Linux) — to exchange information, usually by the creation of a single program that operates on all these operating systems.
  • Allows GTK+ Dialog Box Execution: Variable to use it later when executing the shell script with gtkdialog.
  • Command Line Tool
  • Support in Shell Scripting

Installation of Zenity

Zenity is by default installed or available in the repository of most of the Standard Linux distributions of today. You can check it is installed on your machine or not by executing the following commands.

Check whether zenity installed or not by below command

>> zenity --version

OR

>> whereis zenity 

If it’s not installed, you can install it using the Apt or Yum command as shown below.

On Debian based Systems

>> sudo apt-get install zenity 

On RedHat based systems

 >> sudo yum install zenity 

Moreover, you can also build it from the source files, download the latest Zenity source package (i.e. current version 3.8) using the following link.

http://ftp.gnome.org/pub/gnome/sources/zenity/.

Zenity Basic Dialog Boxes

Some of the basic Dialogs of Zenity, which can be invoked directly from the command line.

  • Calendar
  • File selection
  • Forms
  • List
  • Notification icon
  • Message
  • Error
  • Information
  • Question
  • Warning
  • Password entry
  • Progress
  • Text entry
  • Text information
  • Scale
  • Color selection
  • Many more…

Let’s discuss basic dialog boxes with some use cases, here let’s take as taking information of users and displaying them

Different types of Zenity

Text Entry dialog box

Entry is used to take input from user.

Here we are taking the user's First name and second name storing in variable first_name and second_name.

#First Name
>> first_name=`zenity --title="First name" --entry --text="Enter first name"`
#Second Name
>> second_name=`zenity --title="second name" --entry --text="Enter Second name"`

Second Name

Here we can see User inputs stored in variables

Password, Question, and Warning

Password used to take password text, question prompting to accept or not, a warning will throw a prompt with alert.

Taking user input if a user is a particular country person or else he will not be eligible and storing in file

>> zenity --question --text="Are You Indian citizen" && zenity --password > password.txt || zenity --warning --text="Sorry your not eligible to proceed"

If chosen “YES

Here we can see the password given by the user

Calendar

The calendar is used to select the date, month, and year.

Here we are taking the user DOB.

List option

In this command, we are taking all data stored in variables of users and displaying using the list option

>> zenity --list --title="LOGABLE USERS" --column="FIRST NAME" --column="LAST NAME" --column="PASSWORD" --column="DOB" $first_name $second_name $password $dob --text="User Details" --width="800" --height="400"

Creating Script to Manage Linux Users

This script will perform Creating, Removing, and Displaying all users

Script-1 for creating a new user

Create a file and add below code

>> vim newUser

Script-1 userAdd

  • BashBang( !# ) this will says what to use for running code
  • The below line just stores to userad User name and password given by the user
userad=$(zenity --username --password --title="Login")
  • Both lines will cut the user name and password and store to different variables, -f is for representing column
>> user=$(echo $userad | cut -d"|" -f1)
>> password=$(echo $userad | cut -d"|" -f2)
  • Adding a new user => sudo useradd -m $user
  • Attaching password => echo $password | sudo passwd $user — stdin &> /dev/null
  1. If a user gives password takes or else takes null as password
  2. — stdin takes input as a password and stores
  • Once finished prompts with succsss
zenity — info — title=”User $user created” — text=”SUCCESS”

Change file mode to executable

>> chmod 755 newUser

Below we can see the file before execution mode after execution mode

Executing script to create a new user

>> ./newUser

Before adding USER

Successfully user Created

After Adding a USER

Removing a User Script-2

Create a file for deletion

vim delUser

Change file to execution mode

chmmod 755 delUser

Execute Script-2 to remove a user

Before removing USER

Deleted

After removing user

Thanks for Reading !!

Keep Learning !! Keep Sharing !!

You can contact me on:

LinkedIn emailme

ARTH-School of technology, BCA graduate