Solved

Password as a parameter for a shell script

Posted on 2002-05-20
5
425 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
  • 3
5 Comments
 
LVL 20

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 20

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 20

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

937 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now