jaffinito34 has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to compare the username entered to the username in a file along with the password. The script can check the file and return whether its a match or not but whenever the username does not match, the while loop prints "...does not match..." for each user in the file rather than just once. Here's my code:
#! /usr/bin/perl # CSC 310 Project # 3 #opening passwd file open (PSWD, '<', 'passwd.txt'); #getting username and password #converting username to lowercase if anything is entered in CAPS print "Please enter your username: "; chomp($userN = <STDIN>); $username = lc($userN); print "Please enter your password: "; #hiding password system ("stty -echo"); chomp($passwd = <STDIN>); #reading passwd.txt and assigning values while ($lines = <PSWD>){ ($user,$pswd,$userID,$groupID,$info,$home,$shell) = split ':', $li +nes; #checking username entered vs that in the passwd file if ($username eq $user){ print "Checking username... MATCH\n"; #checking password entered vs that in the passwd file if ($passwd eq $pswd){ print "Checking password... MATCH\n"; } else{ print "Password does not match!\n"; } } else{ print "\"$username\" does not match any users in our database!\n"; } }
The last else statement is what keeps repeating through the while loop but I only want it to print once if the username does not match, not each time that it checks the username against the file. I tried 'last;' but that would kill the while loop immediately so when the username actually does match, it would print that it doesnt because it didnt match the first one in the file. Any help or reference to help is appreciated!
I know the identations are a little off, theyre perfect in my script though
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Checking username and password but cant break whileloop!
by NetWallah (Canon) on Nov 09, 2012 at 16:11 UTC | |
by jaffinito34 (Acolyte) on Nov 09, 2012 at 16:18 UTC | |
|
Re: Checking username and password but cant break whileloop!
by tobyink (Canon) on Nov 09, 2012 at 16:06 UTC | |
by jaffinito34 (Acolyte) on Nov 09, 2012 at 16:10 UTC |