Okay, so I'm trying to create a username/password login script of sorts. (may not be the most secure idea I'm still working on it) ;)
My script will load variables to compare to from file like this. (right now I'm just working on password portion)
./path/to/variables.conf
This file will contain a variable called
PASS=SOME_VALUE
I plan to use read to obtain the variable that will be compared
read -p "Enter your password:" CPASS;
Now the part I'm missing (how I envision it working)
while "$CPASS" doesn't match "$PASS" do
read -p "Wrong password, try again:" CPASS;
Thank you & any help is appreciated.
This should do it. You just need [ ]
braces and the !=
operator to compare strings in bash:
PASS=SOME_VALUE
read -p "Enter your password:" CPASS
while [ "$CPASS" != "$PASS" ]; do
read -p "Wrong password, try again:" CPASS
done
Also note it would be highly advisable to pass the -s
parameter to read, so that the entered password is not echoed back to the user. From the read
section of man bash
:
-s Silent mode. If input is coming from a terminal, char-
acters are not echoed.
Note though that there will also be no newline echoed back to the user when the user hits ENTER, so you'll have to manually insert a newline after every read
so that lines are properly formatted. So you should probably replace your read statements with something like this:
read -s -p "Enter your password:" CPASS
echo