0

I am trying to write a program where the user can enter a username and password, then the code should check if the username and password are correct unfortunately whenever it checks no matter if the username/password is correct of not it will echo "Username Verified".

#!/bin/bash
echo "Username"  
read username  
echo "Password"
read password
sleep 1
correct_u=user
correct_p=pass
if [[ $username -eq $correct_u ]]
then
  echo "Username Verified..."
else 
  echo "Username Incorect..."
fi
sleep 1
if [[ $correct_p -eq $password ]]
then 
  sleep 1
  echo "Password Verified..."
else 
  echo "Password Incorect..."
fi

I have tired checking that all the variables work

2
  • What have you tried to resolve the problem? Where are you stuck? Commented Jan 9, 2023 at 12:54
  • Use this guide for asking good questions that get good, quick answers. Commented Jan 9, 2023 at 14:08

3 Answers 3

1

Unless username and correct_u consist solely of digits, [[ $username -eq $correct_u ]] will always evaluate to true, since -eq forces the arguments to be numbers, and if there are no number, the arguments are treated as zero.

To do a string comparision, do

[[ $username == "$correct_u" ]]

Quoting the right-hand side is important here, to avoid that it is interpreted as glob-pattern, since == in general does a wildcard match.

Sign up to request clarification or add additional context in comments.

2 Comments

The single = does the same, the magic is from [[ ]] afaik.
@Jetchisel: Yes; according to the man page: _The = operator is equivalent to ==. _ It's a matter of taste. I usually prefer the == form, perhaps this is the way many programming languages express equality. But the point of my answer is that you can't use -eq here.
0

You should use = instead of -eq when comparing strings in bash. = is used for string comparison while -eq is used for integer comparison:

#!/bin/bash
echo "Username"  
read username  
echo "Password"
read password
sleep 1
correct_u=user
correct_p=pass
if [[ "$username" = "$correct_u" ]]
then
  echo "Username Verified..."
else 
  echo "Username Incorrect..."
fi
sleep 1
if [[ "$correct_p" = "$password" ]]
then 
  sleep 1
  echo "Password Verified..."
else 
  echo "Password Incorrect..."
fi

1 Comment

if you consider this answer is useful please mark it as accepted :)
0

Embedding your name & pw in cleartext in the file isn't ideal. The user ID must be able to read it to execute the commands in it; executable permissions won't help if you take away read.

Use that to your advantage. Set the user/group/world permissions appropriately. Then the user and password are entered at login...

You might want to combine methods from here and here, reading the right password securely from a vault file and comparing that to the one you read silently from the user.

But first, as already mentioned - fix your test.

$: [[ "one" -eq "two" ]] && echo same || echo no
same

$: [[ "one" == "two" ]] && echo same || echo no
no

$: [[ "one" == "one" ]] && echo same || echo no
same

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.