Table of Contents
Zenith supports a command that allows you to create a password entry dialog. This command is quite useful because when you build the program, somewhere in your program needs to protect the contents of the program. Building a password entry dialog is a necessity.
Use the command
To create a password dialog, zenity uses the --password option. This option is quite simple, it will display a small window so you can enter your password.
zenity --password
Next to the --password option, there is another option, which is --username, which allows you to create a password entry dialog that allows you to enter both a username and a password.
zenity --password --username
After you enter the username and password in the dialog box, the username and password will be displayed on the terminal looks like:
daniepham|writebash.comBash script to create a password entry dialog
Yes, once you have zenity command structure to create a dialog box. I will write a script to create this dialog, the convenience of the bash script is that you can assign variables to username and password values. So you can use those values in your program.
Read more: Create a color selection dialog
The script’s content:
#!/bin/bash
# Script author: Danie Pham
# Script site: https://devopslite.com
# Script date: 07-01-2018
# Script ver: 1.0
# Script use to create a password entry dialog.
# The main function to create dialog
f_create_dialog () {
# Define the command, we use both username and password fields.
ENTRY=`zenity --password --username`
# Read the result from the command above
case $? in
0)
# At this step, you can assign a variable to username's value like this:
# VAR_USERNAME=`echo $ENTRY | cut -d'|' -f1`
# The result from ENTRY command look like: username|password
# This command use to cut the first column -> get username
echo "User Name: `echo $ENTRY | cut -d'|' -f1`"
# This command use to cut the second column -> get password
echo "Password : `echo $ENTRY | cut -d'|' -f2`"
;;
1)
echo "Stop login.";;
-1)
echo "An unexpected error has occurred.";;
esac
}
# The main function of script
f_main () {
f_create_dialog
}
f_main
exitYou copy the script above to your computer, assign permissions to the script and execute to run the script:
chmod +x create_password_dialog.sh
./create_password_dialog.shConclusion
As you can see, using the –password option of zenity to create a password entry dialog is pretty straightforward. With just a few simple commands, you can create a basic authentication layer for your program. This is really useful when you want the program (or program content) to be protected.
(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).
Heya i’m fοr the first time here. I came acroѕs this board and I find It truly helpful & it helped me out ɑ lot.
I am hoping to give something baсk and help others such as you helped me.