Username and password

Hi

I am new to using unix and am struggling with a script i am writing. What i am trying to do is get a user to enter a username, check the original file i created with username and pin to see if their is a corresponding entry. Next ask the user to enter the pin and see if this matches corresponding username. Is it possible to do this by using a cat and grep command or is there another way. You can see the progress I have made below. Any help would be massively appreciated.

Thanks

while :
do
   echo "enter a name"
   read user
   cut -f1 usernames | grep -i -q "$user"
   a=$?
   echo "enter a password"
   read pass
   cut -f2 usernames | grep -q "$pass"
   b=$?

   if [ $a = 0 ] && [ $b = 0 ]
   then
      echo "welcome"
      menu
      exit
   else
      echo "access denied please try again"
   fi
done

The only problem with this is that the password grep does not search for the corresponding username.

Here is the original file i am using:

Username Password

John 123
Dan 345
Matt 678

:wall:

Hi,

The default delimiter for cut is a tab not a space. Use --delimiter=" " to get the second field.

In your code the cut does not return what you think it does.

$cut -f2 usernames 
John 123
Dan 345
Matt 678
while :
do
echo "enter a name"
read user
user_record=$(grep "^$user " usernames)
real_pass=$(echo $user_record|cut -d\  -f2 )
echo "enter a password"
read pass
...
done
#! /bin/bash

while :
do
    echo "enter a name"
    read user
    grep -iq "^$user" usernames
    a=$?
    echo "enter a password"
    read pass
    grep -i "^$user" usernames | grep -q "$pass$"
    b=$?
    
    if [ $a -eq 0 -a $b -eq 0 ]
    then
        echo "Welcome"
        menu # I'm assuming "menu" is a function you haven't pasted here.
        break # "break" is a graceful way of getting out of a loop. Preferred over "exit"
    else
        echo "Access Denied. Please try again"
    fi
done

Thats excellent thank you, and is exactly what im looking for . Just a couple of questions if you dont mind (although im guessing a couple may sound sillly)
1) "^$user" - what does the ^ symbol do?
2)on the part grep -q "$pass$" what does the $ at the end of pass do?

As for menu, you were spot on, it was just a function that i didnt think needed to be pasted here.

Cheers

1) "^$user" - what does the ^ symbol do?
The carat means "line starting with".

2)on the part grep -q what does the $ at the end of pass do?
The dollar means "end of line".

The technique posted is not perfect because you could get multiple matches for similar usernames or similar passwords. e.g. fred and freda . The match strings would be better if they included the delimiter (in this case a single space character):

"^${user} "
" ${pass}$"