Zenity create a password entry dialog

by Daniel Pham
Published: Updated:
This entry is part 5 of 13 in the series GUI for bash script using Zenity

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
Zenity create a password entry dialog
Zenity create password dialog

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
Zenity create a password entry dialog
Create password dialog with both username and password

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.com

Bash 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 moreCreate 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

exit

You 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.sh

Conclusion

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).

0 0 votes
Article Rating
Series Navigation«« Previous part: Zenity create color selection dialogNext part: Zenity create file selection dialog »»

You may also like

Subscribe
Notify of
guest
2 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
boughs
Guest
5 years ago

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.

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

2
0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.