If user input matches file contents


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers If user input matches file contents
# 1  
Old 01-22-2013
If user input matches file contents

I was wondering how I can read values from a file and then proceed if those values
match a user input. For example (a simple one), a user inputs their name, then we check if that name is in a file of usernames. If it is, it says proceed, if not, then it says you are not authorized.

Code:
userlist=`cat /home/user1/user_list.txt`
echo "Enter your username:"
read username
if [[ $username = $userlist ]];then
    echo "You may proceed"
else
   echo "You are not authorized"
exit
fi

userlist.txt:
Code:
bjames
jbryant


Last edited by Scrutinizer; 01-22-2013 at 02:08 PM.. Reason: code tags
# 2  
Old 01-22-2013
One way to achieve this is using grep command:
Code:
echo "Enter your username:"
read username
if [[ $( grep -c "$username" userlist.txt ) -ne 0 ]]; then
   echo "You may proceed"
else
   echo "You are not authorized"
   exit 1
fi

# 3  
Old 01-22-2013
You can simplify that a bit:

Code:
if grep "$username" filename.txt >/dev/null
then
...
fi

# 4  
Old 01-22-2013
Code:
if grep -Fxq "${username:-noname}" filename.txt
then

# 5  
Old 01-22-2013
Thank you all for your solutions. Problem solved. Smilie
# 6  
Old 01-22-2013
You're welcome. Make sure to test what happens when you enter an empty string, or a single dot, or just the letter b, or "james", or special characters, for example...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to search file based off user input then create new file

In the below bash a file is downloaded when the program is opened and then that file is searched based on user input and the result is written to a new file. For example, the bash is opened and the download.txt is downloaded, the user then enters the id (NA04520). The id is used to search... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. UNIX for Dummies Questions & Answers

User input while reading from a file

I am not able to capture the user input in this script(bash).There is prompt for user input.Could some one help me capture user input while reading afile? while read line do echo "$i" path1=$line path2=`echo $line|sed s/new_dir/old_dir/` echo "Do you want to replace?";... (4 Replies)
Discussion started by: parijat guh
4 Replies

3. Shell Programming and Scripting

How to get the user input recursively until the user provides valid input

Hi, echo "Enter file name of input file list along with absolute path : " read inputFileList if then for string in `cat inputFileList` do echo $string done else echo " file does not exist" fi From the above code, if the user enters a invalid file... (1 Reply)
Discussion started by: i.srini89
1 Replies

4. Shell Programming and Scripting

remove contents including the tag if pattern matches

Hi all, Can anyone help me on this. I have several WP sites that are affected by sql injections. But the contents are different as follows western union india belgaum western union india bolegaon western union india barhaj western union india budhana western union india belda western... (6 Replies)
Discussion started by: sanjuabraham
6 Replies

5. Solaris

Processing user input in .sh file

Hi, I am new to shell scripting. script should accept the user value and then compare that value with the null. If null then assign the value "*" to the variable else will use the user inputed value. How to do this ? With Regards (3 Replies)
Discussion started by: milink
3 Replies

6. UNIX for Dummies Questions & Answers

how to place input from user into a file

as the title, how can i allow a user to key in one's name, birthday and email address and save it into a specific file i have created earlier? thank you so much for any reply. (3 Replies)
Discussion started by: branred
3 Replies

7. Shell Programming and Scripting

Take user input and then append to another file

Hi, I have below line from the config file $__MR_CONFIG = 'userinput'; I want to get the value from the user for the "userinput" and then append the full line to another file. Can you please let me know how can I proceed on this? I am new to unix and your help is greatly appreciated.... (3 Replies)
Discussion started by: nav123
3 Replies

8. Shell Programming and Scripting

Create Multiple files by reading a input file and changing the contents

Being new to this area .I have been assigned a task which i am unable to do . Can any one please help me . Hi I have requirement where i have input file XYZ_111_999_YYYYMMDD_1.TXT and with header and series of Numbers and Footer. I want to create a mutiple output files with each file having a... (2 Replies)
Discussion started by: bhargavkr
2 Replies

9. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies

10. Shell Programming and Scripting

validate input from user for file name

Hello, This may have been addressed already somewhere, however I am looking for the easiest/shortest way to validate a response from a user for a file name. The file name should not have any of the following characters ~`!@#$%^&*()_-+={|\:;"'<,>.?/ Further the response should not have any... (2 Replies)
Discussion started by: jerardfjay
2 Replies
Login or Register to Ask a Question