Solved

Password as a parameter for a shell script

Posted on 2002-05-20
5
423 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
Comment Utility
#!/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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.

771 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

10 Experts available now in Live!

Get 1:1 Help Now