LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Password program in unix (https://www.linuxquestions.org/questions/linux-newbie-8/password-program-in-unix-4175456789/)

jags1984 04-03-2013 11:47 PM

Password program in unix
 
Hi,

I wanna write password program in unix

Code:

stty -echo
read ch
stty echo

I want to display '*' whenever the user eneters a character. The above code mask the password totally and nothing is displayed on screen

millgates 04-04-2013 02:12 AM

You can use read -s instead of stty. If you want those *, though, you will probably have to implement that yourself. Something like (pure bash) could work:

Code:

pass=''
while read -s -r -N 1 t; do
    case "$t" in
        $'\b' ) echo -en $'\b \b'
          pass="${pass%?}" ;;

        $'\0' ) echo
                break ;;
        *) pass+="$t"
          echo -n '*' ;;
    esac;
done

echo "your password is: '$pass'"

Not exactly a oneliner, but you may put it in a function.
Maybe the more advanced languages (Perl etc.) have a function for that, though.

jags1984 04-04-2013 04:33 AM

Thanks for the response,


I am new to shellscript and not able to grab the syntax, can you pls tell me what is doing

What I understood : We are reading the input character by character and in case statement checking the characters.

I am not able to catch the case statement.


Backspace is also taken as input, I want to make the backspace and DELETE keys to behave normal way.

millgates 04-04-2013 04:59 AM

OK, so first, I used read with some arguments:
-s for silent input
-r to disable interpreting of backslashes
-N 1 to read one character each time

The case statement is quite simple:
The first case handles backslashes: it removes one * from the prompt.
pass="${pass%?}" removes the last character from the string

The second case terminates the loop when you press ENTER

The last case is for all other characters: the character read is appended to $pass variable

The backspace should work as expected (it does for me), but it may depend on your terminal settings.
As for DELETE, you would have to add special case for that. But that would also imply handling the arrow keys and the work with the string would get much more complex. And I don' think it would be very useful since you can't see what you type anyway.

chrism01 04-04-2013 08:39 PM

You may also want to read these
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

jags1984 04-05-2013 04:18 AM

Is there any password program in shell script which handles all possibilties such as left arrow, right arrow, delete, backspace etc

I want my password program to read only characters, spcial characters and numbers like a standard password program.

TB0ne 04-05-2013 09:03 AM

Quote:

Originally Posted by jags1984 (Post 4925569)
Is there any password program in shell script which handles all possibilties such as left arrow, right arrow, delete, backspace etc
I want my password program to read only characters, spcial characters and numbers like a standard password program.

Why don't you post what you've written/tried so far, and what results you're getting? This sounds very much like a homework question, and so far, you've not shown that you've done any of it.

Try looking at the man page for the script command, it may do what you want.


All times are GMT -5. The time now is 07:13 AM.