LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-04-2013, 12:47 AM   #1
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Rep: Reputation: Disabled
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
 
Old 04-04-2013, 03:12 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
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.
 
1 members found this post helpful.
Old 04-04-2013, 05:33 AM   #3
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
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.
 
Old 04-04-2013, 05:59 AM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
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.

Last edited by millgates; 04-04-2013 at 06:05 AM.
 
Old 04-04-2013, 09:39 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
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/
 
Old 04-05-2013, 05:18 AM   #6
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
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.
 
Old 04-05-2013, 10:03 AM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,948

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by jags1984 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get the login/password for a donated unix pc jkawah Linux - Desktop 7 08-14-2007 04:38 PM
UNIX password: fransd Linux - Software 1 11-01-2006 05:42 PM
unix password security securitycheck Other *NIX 2 10-07-2004 01:07 AM
Just change Samba password, not Unix password? sorrodos Linux - Networking 1 08-14-2003 08:52 PM
SAMBA UNIX password synchronization sewer_monkey Linux - Networking 2 10-30-2002 12:38 PM


All times are GMT -5. The time now is 12:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration