How to add more username and passwords here


The contents inside the bash file will be obfuscated

echo "Enter The Login Name:"
read username
echo "Enter The Password:"
read password

if [[ $username == "user"  && $password == "password" ]]
  echo -e "You're Logged In\n"
elif [ $username != "user" ] 
  echo -e "Invalid User Name\n"
  echo -e "Invalid Password\n"

Best Answer

One of the comments says:

Create text file with usernames and passwords. And via loop check them from file.

It's a decent idea. The code may be like this:


function validate {
    local user pass
    while IFS=' ' read -r user pass; do
       [ "$1" = "$user" ] && [ "$2" = "$pass" ] && return 0
    done </the/file
    return 1

read -rp "Login: " u
read -rsp "Password: " p

if validate "$u" "$p"; then
   echo "Access granted."
   echo "Access denied."

Example /the/file:

kamil foo
joe bar
anotherUser pa$$word
bill password that contains spaces

Everything after the first space in a line is the password.


  • This could be done without a function. The function is quite useful here because of its ability to return 0 (success) from the middle of the loop without additional logic.
  • An empty line means empty login and empty password. Note in *nix every line in a text file should end with a newline character, so a newline character at the very end of the file doesn't mean there is an empty line after it. Still it's quite easy to put a truly empty line at the end so pay attention. Alternatively you can add some logic to the function so empty login will never be accepted.
  • To run this code without errors, you need to have read access to /the/file. But if you do, you can just read the file and know all secrets. I won't fix this here. Storing passwords in a secure manner is beyond the scope of the question.
  • If you prefer to embed passwords in the shell file itself then use here document. Instead of done </the/file do like:

       done <<'EOF'
    kamil foo
    joe bar
    anotherUser pa$$word
    bill password that contains spaces

    Note quoting the first EOF is important. Without it $$ would be expanded.