Solved

Password as a parameter for a shell script

Posted on 2002-05-20
5
432 Views
Last Modified: 2010-04-21
Hello,

I have a shell script that accepts parameters. One of them is a password and I would like not to enter the password as a parameter, as it appears in clear on the command line. The password should be typed by the user and if possible invisible on screen. How can I do this?

Thanks.
0
Comment
Question by:ornicar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 21

Expert Comment

by:tfewster
ID: 7021711
#!/usr/bin/ksh
echo "Enter password"
trap "Sorry, Ctrl-C disabled" 2
stty -echo
read PW
echo $PW
stty echo
trap 2
0
 
LVL 21

Expert Comment

by:tfewster
ID: 7021727
Of course, you'll have noticed that the "trap" statement is wrong and you wouldn't normally echo the password back after taking steps to prevent it displaying on screen...
0
 

Accepted Solution

by:
tryno earned 100 total points
ID: 7060502
I had a similar question myself some time ago, and I got a very good answer form expert99:


#!/bin/sh

# save old settings

OLD_STTY=`stty -g`

# stop all the fancy character processing, read a character at a time

stty -echo -icanon min 1

# loop twice for confirmation

for LOOP in 1 2
docase "$PASSWORD" in
               "")echo -n "Enter Password:   "
               ;;
               *)PASSWORD1="$PASSWORD"
               PASSWORD=""
               echo -n "\nConfirm Password: "
               ;;
       esac

       # get the password

       while CHAR=`dd bs=1 count=1 2>/dev/null`
       do
               case "$CHAR" in
                       "")break
                       ;;
                       *)echo -n "*"
                       PASSWORD="$PASSWORD$CHAR"
                       ;;
               esac
       done

done
# reset the character processing to original state

stty "$OLD_STTY"

# move cursor down

echo ""

if [ "$PASSWORD1" = "$PASSWORD" ]
then
       echo "Match"
else
       echo "No Match"
fi

0
 
LVL 21

Expert Comment

by:tfewster
ID: 7080950
Nice script - filing it for future use ;-)

ornicar, does that answer your question? If so, please accept trynos comment as an  answer. If not, please post more information.
0
 
LVL 9

Author Comment

by:ornicar
ID: 7081013
tryno's is nice. I'll accept his answer. But yours, tfwefters, deserves to be very simple and I prefer it for this reason, so I made a question for you.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Best way to handle awk return value 6 75
Shell script errors 10 143
auto mounter on solaris 1 73
Expiration and Locking of Unix/Solaris OS Accounts 7 65
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question