Beginner Shell Programming Question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Beginner Shell Programming Question
# 1  
Old 02-26-2010
Beginner Shell Programming Question

Hello all,

I am currently try to learn the linux operating system as well as some bash programming. I have come across some online course work which has been very helpful, I have been working through some assignments and since I have no teacher to ask I have come to you experts.

So the program I am trying to write, asks the user to input a username and password. I have 3 combos that I came up with, and when a user gets them right you get a welcome. When you get it wrong I want it to loop for them to try again.

I can do this when it is just one variable, say enter password. But I am not sure how to do this when you have a username and password and you then want to verify against stored variables. Please excuse me if my english isnt the best at explaining.

Here is my code so far.

Code:
#!/bin/bash

user1=Jill
user2=Bob
user3=Jim

pass1=Apple
pass2=Orange
pass3=Lemon

echo ""
echo -n "Username: "
read USER
echo -n "Password: "
read PASS

if [ "$USER" = "$user1" ] && [ "$PASS" = "$pass1" ]; then
        echo "Congrats your in"

elif [ "$USER" = "$user2" ] && [ "$PASS" = "$pass2" ]; then
        echo "Congrats your in"

elif [ "$USER" = "$user3" ] && [ "$PASS" = "$pass3" ]; then
        echo "Congrats your in"

else    
    echo "Please Try Again"

    read USER
    read PASS

    
fi


exit 0

So I know that it is not correct, as any incorrect combo u enter wont let you try again to input ur choice. But I do not understand why? Could someone shed some light on this for me. I have looked at many resources and have not been able to answer.

Thank you very much for your time

Victor
# 2  
Old 02-26-2010
MySQL Solution

Use a loop ,in that using break and continue statement you can achieve it

Code:
user1=Jill
user2=Bob
user3=Jim

pass1=Apple
pass2=Orange
pass3=Lemon

while [ 1 ];
do
echo ""
echo -n "Username: "
read USER
echo -n "Password: "
read PASS

if [ "$USER" = "$user1" ] && [ "$PASS" = "$pass1" ]; then
        echo "Congrats your in"
        break

elif [ "$USER" = "$user2" ] && [ "$PASS" = "$pass2" ]; then
        echo "Congrats your in"
        break

elif [ "$USER" = "$user3" ] && [ "$PASS" = "$pass3" ]; then
        echo "Congrats your in"
        break

else
    echo "Please Try Again"
    continue

fi
done

# 3  
Old 02-26-2010
I did small changes in you code.This code will do the required thing for you.
Code:
 #!/bin/bash  user1=Jill user2=Bob user3=Jim  pass1=Apple pass2=Orange pass3=Lemon  while true do echo -n "Enter Username: " read USER echo -n "Enter Password: " read PASS  if [ "$USER" = "$user1" ] && [ "$PASS" = "$pass1" ]; then         echo "Congrats your in" 	break  elif [ "$USER" = "$user2" ] && [ "$PASS" = "$pass2" ]; then         echo "Congrats your in" 	break  elif [ "$USER" = "$user3" ] && [ "$PASS" = "$pass3" ]; then         echo "Congrats your in" 	break else         echo "Please Try Again."  fi done  exit 0

# 4  
Old 02-26-2010
karthigayan

Thank you very much for the solution, by chance may you explain the line

Code:
while [ 1 ];
do

What exactly is that doing? I would like to learn this very much. Thank you again for your help I eagerly await your response.
# 5  
Old 02-26-2010
Please find the modified script. I have added a while loop so that the loop won't exit till the user logs in using correct username and passwd.

Code:
#!/bin/bash

user1=Jill
user2=Bob
user3=Jim

pass1=Apple
pass2=Orange
pass3=Lemon


i=1

while [ $i -eq 1 ];do

echo ""
echo -n "Username: "
read USER
echo -n "Password: "
read PASS

if [ "$USER" = "$user1" ] && [ "$PASS" = "$pass1" ]; then
        echo "Congrats your in"
i=$(( $i + 1 ))
elif [ "$USER" = "$user2" ] && [ "$PASS" = "$pass2" ]; then
        echo "Congrats your in"
i=$(( $i + 1 ))
elif [ "$USER" = "$user3" ] && [ "$PASS" = "$pass3" ]; then
        echo "Congrats your in"
i=$(( $i + 1 ))
else    
    echo "Please Try Again"

    

    
fi
done

exit 0

# 6  
Old 02-26-2010
while [ 1 ]
This stands for infinite loop.
This condition will be always evaluated to true.
This is similar to while [ true ]
# 7  
Old 02-26-2010
Login

Try this,

Code:
#!/bin/bash

user1=Jill
user2=Bob
user3=Jim

pass1=Apple
pass2=Orange
pass3=Lemon


while [ 1 ];do

        echo ""
        echo -n "Username: "
        read USER
        echo -n "Password: "
        read PASS

        if ( [ "$USER" = "$user1" ] && [ "$PASS" = "$pass1" ] ) || ( [ "$USER" = "$user2" ] && [ "$PASS" = "$pass2" ] ) || ( [ "$USER" = "$user3" ] && [ "$PASS" = "$pass3" ] ); then

                echo "Congrats your in"
                break;

        else
                echo "Please Try Again"


        fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

C shell - a question of a beginner

Hi, can someone explain how this aliases work: alias m 'if ( \!:$ =~ *gz) set status = 1 && most \!:$ || gzcat \!:1 | most ' I mean, I know what it does, but I don't know how - what is \!:$ and \!:1 - how do it used the && and || - what it does with status variable? thanks (3 Replies)
Discussion started by: tzvikao
3 Replies

2. Shell Programming and Scripting

Shell Programming (beginner help)

So guys basically I was really sick and couldn't attend the labs and lectures and I went to my lecture hoping he would say ok I will help you from the start but he just said google it. So If it's possible to make the assignment and explain more in detail why is that would be really helpfull. I can... (1 Reply)
Discussion started by: Joola94
1 Replies

3. Shell Programming and Scripting

A beginner needing some help programming documents

Hi all, I'm a fairly new beginner with shell programming and python programming. I have a mac (mountain lion OS 10.8.2) and use the terminal for programming. I'm trying to use the unix to easily organize some language data that I am working with. Basically I have to word lists, that I need to... (2 Replies)
Discussion started by: nomadblue
2 Replies

4. Programming

Shell programming ksh AIX - beginner

Hi! I have two shell scripts - Script1, Script2 Script1, Script2 - have return parameter Script1 - is calling Script2 in Script2 I am calling program sqlldr - if this program is called then I did not get the return parameter from Script1 Do You have any idea how can I avoid this problem. Mroki (6 Replies)
Discussion started by: mroki
6 Replies

5. Shell Programming and Scripting

Plz Help Me in This question in Shell Programming

2- Write a bash shell script filestatic. The script should examine the number files in directories given as arguments (parameters) to this script. a. if one argument is given, the script should count and report the number of files in this directory. Only regular files should be counted, not... (1 Reply)
Discussion started by: tahseen_22334
1 Replies

6. Programming

beginner to c programming

hii friends i m fairy new to c programming.can any one suggest some good websites and some good books for beginner (6 Replies)
Discussion started by: pankajchandel
6 Replies

7. Shell Programming and Scripting

question about testing in shell programming

In folder A i have a file "a' and text file named infile00.I would like to do redirection :a<infile01. There is a code to do this #get a file "a" in /home/A for file in `ls /home/A` do if then #printing out if file is an execute file echo $file "is an execute... (6 Replies)
Discussion started by: thungmail
6 Replies

8. Shell Programming and Scripting

question about testing in shell programming

Hi i would like to write a "script" which takes a directory as an argument and the script will output the content of a file in this directory.Here is my code #!/bin/sh #set an argument to be a specified path $1=/home/tuan/Desktop/Shell_programming/directory #check if an argument is a... (3 Replies)
Discussion started by: thungmail
3 Replies

9. Shell Programming and Scripting

question about about Shell programming

1.if 2.then 3. # save the number of args and first argument in variables. 4. num_args=$# 5. id="$1" 6. echo "$id" 7. #echo "$1" > crapfile.txt 8. echo `sed 's/\*/'\*'/g' < crapfile.txt` Above is a partial code.I would like to ask: at line 1:... (1 Reply)
Discussion started by: thungmail
1 Replies

10. Shell Programming and Scripting

Question about Shell Programming

First, I'd like to know if being a shell programmer considered a "real" programmer. is it?? also, I do create a lot of shell programs which includes full scripts to create users and maintaining records. ie phone records. now, I hear the programmer has to do some cleaning up after the... (3 Replies)
Discussion started by: IMPORTANT
3 Replies
Login or Register to Ask a Question