A password box

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to: navigation, search
← An input dialog boxHomeA menu box →
  • A password box is just like an input box, except that the text the operator enters in to displayed on screen.
  • Use this to collect user passwords.
  • The "--insecure" option will display password as sting.
  • On exit, the input string will be printed on dialog's output.

Example

  • Create a shell script called getpasswd1.sh:
#!/bin/bash
# getpasswd1.sh - A sample shell script to read users password.
# password storage
data=$(tempfile 2>/dev/null)
 
# trap it
trap "rm -f $data" 0 1 2 5 15
 
# get password
dialog --title "Password" \
--clear \
--passwordbox "Enter your password" 10 30 2> $data
 
ret=$?
 
# make decision
case $ret in
  0)
    echo "Password is $(cat $data)";;
  1)
    echo "Cancel pressed.";;
  255)
    [ -s $data ] &&  cat $data || echo "ESC pressed.";;
esac

Save and close the file. Run it as follows:

chmod +x getpasswd1.sh
./getpasswd1.sh

Sample outputs:

The --insecure option

  • The --insecure option makes the password widget friendlier but less secure, by echoing asterisks for each character.
  • Create a shell script called getpasswd2.sh:
#!/bin/bash
# getpasswd2.sh - A sample shell script to read users password.
# password storage
data=$(tempfile 2>/dev/null)
 
# trap it
trap "rm -f $data" 0 1 2 5 15
 
# get password with the --insecure option
dialog --title "Password" \
--clear \
--insecure \
--passwordbox "Enter your password" 10 30 2> $data
 
ret=$?
 
# make decison
case $ret in
  0)
    echo "Password is $(cat $data)";;
  1)
    echo "Cancel pressed.";;
  255)
    [ -s $data ] &&  cat $data || echo "ESC pressed.";;
esac

Sample outputs:

A password dialog box with the --insecure option
← An input dialog boxHomeA menu box →