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.

#!/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

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

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

I did small changes in you code.This code will do the required thing for you.

 #!/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 

karthigayan

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

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.

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.

#!/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

while [ 1 ]
This stands for infinite loop.
This condition will be always evaluated to true.
This is similar to while [ true ]

Try this,

#!/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