0

I'm new to shell, I want to do the following task

A shell program which accepts username and password and checks the $username and $password with a line in another file (for authentication), I know how to get the username from user but how do I check against a line in another file. say I have a file called username (which has the username) and file called password (which has the password) how do I check $username with that line and $password with that line in the file

2
  • can you provide samples of your files please? Commented Jun 10, 2014 at 7:46
  • username.txt file has a single line "admin" and password.txt file has a single line "password" now how do I compare a string $username with the word in the file named username.txt Commented Jun 10, 2014 at 7:55

3 Answers 3

1

Here is an example script which prompts the user to enter a username and password and then compares them against the username and password taken from two files:

#!/bin/bash

# prompt the user to enter a username and password
read -p "Username: " inputUsername
read -s -p "Password: " inputPassword

# read the username and password from file
username=$(<username.txt)
password=$(<password.txt)

# compare
if [[ "$inputUsername" == "$username" && "$inputPassword" == "$password" ]]
then
    echo "valid username and password"
else
    echo "invalid username and password"
fi
Sign up to request clarification or add additional context in comments.

4 Comments

@dogbane how does username=$(<username.txt) work ?
@Jidder username=$(<username.txt) in that dump the username from username.txt and saved it in usename variable.
@Jidder It is Command substitution. Look here it will clear your doubt wiki.bash-hackers.org/syntax/expansion/cmdsubst#specialities
So that command just dumps the entire contents of username.txt into username then ?
0

Straight from the perl cookbook: (I guess it's no good though because the OP wanted bash)

#!/usr/bin/perl -w
# 

checkuser - demonstrates reading and checking a user's password

use Term::ReadKey;

print "Enter your password: ";
ReadMode 'noecho';
$password = ReadLine 0;
chomp $password;
ReadMode 'normal';

print "\n";

($username, $encrypted) = ( getpwuid $< )[0,1];

if (crypt($password, $encrypted) ne $encrypted) {
    die "You are not $username\n";
} else {
    print "Welcome, $username\n";
}

Comments

0

Super basic and has no security but this should work for finding them in a file

How i think it should be done(single file)

echo 'Type username'
read username
echo 'Type Password'
read -s Password
awk /^$username.*$Password$/' {print "found"}' loginfile

For the way OP has files set up

echo 'Type username'
read username
echo 'Type Password'
read -s Password
UserCheck=$(awk /^$username$/' {print "1"}' Username.txt)
PassCheck=$(awk /^$Password$/' {print "1"}' Password.txt)

if [[ UserCheck -eq 1 && PassCheck -eq 1 ]]; then
     echo "Found"
else
     echo "not found"
fi

4 Comments

You should match the start and end of the line in your awk pattern. Otherwise, if the user enters "pass" and the file has "password123" it will succeed.
the issue affects both
It will only match in the first if the line contains both username and password ? The second both must be found for it to succeed and say found ? I dont understand what you mean ?
@dogbane just realised what you meant. Ammending it now

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.