Saturday, October 23, 2010

Reading passwords in shell script

Today One BE final year student just asked me this one on IM, for his benefit and anyone who might want to have a password wrapper(if at all!!) here it is how to do the same via a shell script,

#!/bin/sh
###This is a simple bash screipt to read password###
######without echoing the same on console#####

actual="deburocks!"
failed=0
while [ $failed -le 3 ]
do
read -s -p "Password: " userpasswd
echo ""
if [ "$userpasswd" == "$actual" ]
then
echo "Password accepted"
read
exit 1
else
echo "Sorry! Wrong password, Try again..!!"
fi
failed=$(( $failed + 1 ))
done

Please remember - 'stty' can also do the trick as below:

stty -echo
read password
stty echo

Enjoy!!

No comments:

Post a Comment

8c tips for Freshers / NCG's

I have been having a great company with some of the very talented NCG’s ( New College Hires/ Grads ) last ~2yrs.  I work for Intuit I...