LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-30-2012, 08:22 AM   #1
dsmith8890
LQ Newbie
 
Registered: May 2012
Posts: 4

Rep: Reputation: Disabled
Exclamation if statement in a for loop


HI im trying to run this loop so that it will only ask the user to enter his details 3 times before it exits the program if the details are incorect. I have managed to use a while loop, but this does not achieve exactly what im after! does anyone have any ideas. Just to clarify im using Bash Unix.

Thanks

Code:
#!/bin/bash

while :
do
echo "please enter you username"
read user
grep -iq "^user" usernames
a=$?
echo "please enter your password"
read pass
grep -i "^$user" usernames | grep -q "$pass$"
b=$?

if [ $a -eq 0 -a $b -eq 0 ]
then
echo "welcome"
break
else
echo "please try again"
fi
done
 
Old 05-30-2012, 08:46 AM   #2
dsmith8890
LQ Newbie
 
Registered: May 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
is this even possible? sorry but i am new to this!!
 
Old 05-30-2012, 09:25 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Why not just use a counter and have the while loop test if correct or a count of 3?

Are we to understand that you have a plain text file containing user names and passwords (not very safe by the way)?
 
Old 05-30-2012, 10:07 AM   #4
dsmith8890
LQ Newbie
 
Registered: May 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks that seems to work. Your right, although it doesnt actually hold the correct passwords its only a sample programe as im just practicing.
 
Old 05-30-2012, 12:53 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Your right, although it doesnt actually hold the correct passwords its only a sample programe as im just practicing.
No problem ... just a warning

If you have solved your problem, please use the thread tools to mark it as such.
 
Old 05-30-2012, 03:51 PM   #6
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
What are you trying to accomplish here? Asking a user for their password in bash itself is *very* scary.
 
Old 05-31-2012, 01:29 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I don't think it's that bad if, for example, it's a wrapper script for a command that uses a password anyway. The values aren't available outside of the script's process,and when it terminates they will be lost. More worrisome is storing them in a plain text file, as has already been mentioned.


If I may give a bit of general advice for our new scripter, however...

1)
Clean, consistent formatting makes code readable and more easily debuggable. Indent all your sub-commands, and separate logical sections with whitespace. Add comments anywhere the code isn't completely obvious (and remember, what seems obvious to you now will not be so a year or so down the line).

Many people also feel that it's more readable to place the "do/then" keywords on the same line as the "for/while/until/if" keywords, as it more clearly separates the outside block from the inside block.

Code:
for var in fee fai foo fum ; do

	if [[ "$var" == "foo" ]]; then
		echo "Found 'foo'."
	fi

done

2)
When using bash or ksh, it's recommended to use ((..)) for numerical tests, and [[..]] for string/file tests and complex expressions. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://mywiki.wooledge.org/ArithmeticExpression
http://mywiki.wooledge.org/BashFAQ/031
http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression


Otherwise, it's looking pretty good.
 
  


Reply



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
A for loop in an if statement tcsh leavage Programming 2 08-17-2011 11:34 PM
A for loop in an if statement tcsh leavage Programming 2 08-17-2011 11:32 PM
Need help with for loop + if statement Thaidog Programming 5 05-06-2011 09:54 PM
can't put an if statement as an increment in a for loop. Why? japhy Programming 2 02-07-2009 06:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:03 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
Open Source Consulting | Domain Registration