Zenity create a password entry dialog

Share this post to your friends !
  • 1
    Share

This entry is part 5 of 13 in the series GUI for bash script using Zenity
Zenity create a password entry dialog
5 (100%) 2 vote[s]

WriteBash - 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-01 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-02 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://www.writebash.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.

Continue reading the series«« Previous part: Zenity create color selection dialogNext part: Zenity create file selection dialog »»
If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.


Share this post to your friends !
  • 1
    Share
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments