matching user input to a text file loop?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting matching user input to a text file loop?
# 1  
Old 11-28-2011
matching user input to a text file loop?

Code:
       
until [ $cars1 -eq cars_names.txt ]
do
read -p "Invalid cars. Try againa" cars1
done

Ok i have the above code, im getting users input and if it doesnt match in the file the user has to try again untill its correct

But when i run this it gives me an error saying

Code:
./Cars.bash: line 43: [: ford: integer expression expected

# 2  
Old 11-28-2011

You are using a numeric comparison (-eq) for strings. Use string comparison:
Code:
until [ "$cars1" = cars_names.txt ]
do
  read -ep "Invalid cars. Try again: " cars1
done

This User Gave Thanks to cfajohnson For This Post:
# 3  
Old 11-28-2011
Assumption: Your trying to validate user input against a list of valid responses (in cars_names.txt).

Code:
read -p "Car: " cars1
until grep -wiq "$cars1" cars_names.txt && [ -n "$cars1" ]
do
   echo "Invalid car. Try again"
   read -p "Car: " cars1
done


Last edited by Chubler_XL; 11-28-2011 at 07:26 PM.. Reason: Added trap for empty response
This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Would like to check user input for letters within a loop

Hi All, #!/bin/bash #Just trying to check if letters are in the user input. Any tips? # I have tried regexp and using 0-9 etc, i cannot get this to work either in just an if statement or while in a loop. echo "Please pick a number" read num if ; then echo "Please enter a number"... (7 Replies)
Discussion started by: jvezinat
7 Replies

2. Shell Programming and Scripting

Unable to read user input inside a loop

Hi, This query is a part of a much more lengthy script. I wish to look for all the files in a folder named "data" which in this case has two files i.e. plan.war and agent.properties. For all the files found under data I wish to ask the user as to where they wish copy the files to. Below,... (14 Replies)
Discussion started by: mohtashims
14 Replies

3. Shell Programming and Scripting

Loop breaks on yes/no user input

I have a shell script, and its pretty much done, I decided to add a loop that ends or continues depending on user input. like "would you like to continue?" and if I hit y or yes it will run the loop again until I hit n or no and breaks out of the loop. To be hones I didn't think I needed to add... (2 Replies)
Discussion started by: Demon_Jester
2 Replies

4. Shell Programming and Scripting

no chance to input passwd when create new user in loop

Hi Dears, I have one script to create new users with information in one plain text file. This script will read all lines in the file and create one users for one line. Sample file: #action;login,full name title,expire date,project +;gmwen,Bruce Wen QA,04/01/2012,BT +;xxdeng,Shown Deng... (4 Replies)
Discussion started by: crest.boy
4 Replies

5. Shell Programming and Scripting

Input file line matching

Hi there, I have two input files. I need to match the last two columns of each line in File1 with the first two columns of a line in File2. Example: File1 AAA BB 234 789 BBB CC 426 624 CCC DD 356 643 File2 766 332 12 234 789 64 122 633 23 426 624 88 777 453 22 356 643 92 ... (3 Replies)
Discussion started by: palex
3 Replies

6. UNIX for Dummies Questions & Answers

How to read a line of text from user input?

Hiii I wanna a read a line of text from standard input. The user enter data like this way name phone_no month1_salary month2_salary that is user enter the name ,phone no and salary of 2 months in a single line by giving spaces. I wanna add the 3rd and 4th fields ...ie add both... (4 Replies)
Discussion started by: krishnampkkm
4 Replies

7. UNIX for Dummies Questions & Answers

Getting user input from inside a while loop?

I'm new to BASH and i'm trying to create a script which is simply put a large find and replace file. This is what I have so far N=0 while read LINE ; do N=$((N+1)) sed 's/'$2'/'$3'/g' $LINE > .temp echo "Changes to file $N = $LINE" echo 'The following changes... (5 Replies)
Discussion started by: Azumandious
5 Replies

8. UNIX for Dummies Questions & Answers

Replacing part of a text file with user input.

Ok, I am brand new to UNIX and I am trying to learn a cross between basic script and database use. I had got some ideas off the net on simple ideas for learning UNIX. I am working on creating a simple phone book program that allows myself to enter our employees from work into a phone book text... (0 Replies)
Discussion started by: georgefurbee
0 Replies

9. Shell Programming and Scripting

Loop until user input is correct

Hello, i know how to retrieve a user input (read), and how to manage the different options (case statement). But... could anybody show me a script that, if the user option is incorrect, don't allow to continue the excution, i.e., if the value entered is not 1 or 2, the script shows a question. ... (2 Replies)
Discussion started by: aristegui
2 Replies

10. UNIX for Dummies Questions & Answers

read user input from within a wile loop that is being fed from below

hi! i need to do a ksh script that uses a wile loop that is fed form below while read line do some things done < myfile inside the while loop i need to read user input to ask the user what he wants to do, but "read" reads the file, and not the standard input while read line do ... (2 Replies)
Discussion started by: broli
2 Replies
Login or Register to Ask a Question