Solved

Password as a parameter for a shell script

Posted on 2002-05-20
5
433 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
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…
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 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…

717 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