Compare user string with and predefined file...

i am trying to write shell script
how to write shell script which is ask for user id and password then compare both from two different file..
i have no idea how to compare with data present in file..

#!/bin/bash -e

read -p "User ID: " first
read -p "Password: " second

if [ X$first = X$second ];then
	echo "Both Values are equal"
else
	echo "Mismatch"
fi

Hi niko420,

Try this:
if [ $first == "abc" ] && [ $secound == "efg" ]
then
echo "Match"
else
echo "MisMatch"
fi

You will have to extract the information from the two files:

#! /bin/bash -e

read -p "User ID: " userid
stty -echo
read -p "Password: " password
stty echo

userkey=$(getuserkey "${userid}")
userpwd=$(getuserpwd "${userkey}")

if [[ -n "${userkey)" -o "${password}" != "${userpwd}" ]]; then
  echo Invalid username or password
  exit 1
fi

(shell psuedo-code, untested!)

Please keep in mind that both files would have to be readable by all users of this script, and the "password" would be stored as cleartext. You could set the script to run setuid, but that could raise other security issues. Perhaps man sudo (All) could be used to validate access, just have the script run:

sudo -u safeuser /path/to/therealscript

grep can be handy to search for values in an external file, if you had a you file like user.dat like this:

guest:paSSword
reports:n0P

You could use it something like this

#!/bin/bash

read -p "User ID: " USER_ID
read -p "Password: " USER_PASS

if grep -qx "$USER_ID:$USER_PASS" user.dat
then
	echo "Both Values are equal"
else
	echo "Mismatch"
fi

Arguments for grep -q no output just set return code if found.. -x match entire line

Note: there are much better and safer ways to validate users against passwords and this is just an example of matching data against a file.