?
Solved

Password as a parameter for a shell script

Posted on 2002-05-20
5
Medium Priority
?
435 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 400 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

Technology Partners: 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

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
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.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

764 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