Visit Our UNIX and Linux User Community

checking users input to file

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting checking users input to file
# 1  
Old 11-27-2011
checking users input to file

ok im sorta stuck on this,

The user enters a car model and it has to check it in a text file in the current directory. If the car model is not in the file, user has to enter another model

This is what i have so far

read -p "Enter a car model: " car1
grep -w $car1=$(cat carMakes.txt)
test $? = 0 && echo "Car not found, try again please"

it executes but i get a long list saying, the following

grep: Honda No such file or directory

# 2  
Old 11-27-2011
read -p "Enter a car model: " car1
grep -w $car1 carMakes.txt
test $? -ne 0 && echo "Car not found, try again please"

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 11-27-2011
ok thanks that works but say the first time i enter something wrong then i enter it wrong again, how do i keep making it loop until something correct is entered?

I changed the echo in the third line to read -p and added the variable at the end
read -p "Enter a car model: " car1
grep -w $car1 carMakes.txt
test $? -ne 0 && read -p "Car not found, try again please" car1

also when i enter the model name and its correct it repeats it on the next line

Last edited by gangsta; 11-27-2011 at 10:00 PM..
# 4  
Old 11-28-2011
Use grep option -q to suppress output.

#! /bin/bash

while [ 1 ]
    read -p "Enter car model: " car1
    grep -wq $car1 carMakes.txt
    if [ $flag -eq 0 ]
        echo "Car name not found in file. Enter again."

Previous Thread | Next Thread
Test Your Knowledge in Computers #68
Difficulty: Easy
Awk is abbreviated from the names of the developers - Aho, Weinberger, and Kernighan.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Checking the user input in perl for characters and length

My question is basically as the title says. How can I check a user inputted string is only certain characters long (for example, 3 characters long) and how do I check a user inputted string only contains certain characters (for example, it should only contain the characters 'u', 'a', 'g', and 'c')... (4 Replies)
Discussion started by: Eric1
4 Replies

2. UNIX for Dummies Questions & Answers

Best Alternative for checking input parameter contains required value or not

Any good way to check if code has the required output # /sbin/sysctl net.ipv4.icmp_echo_ignore_broadcasts net.ipv4.icmp_echo_ignore_broadcasts = 1 /sbin/sysctl net.ipv4.icmp_echo_ignore_broadcasts | grep "= 1" net.ipv4.icmp_echo_ignore_broadcasts = 1 What I can think of is above, and it... (16 Replies)
Discussion started by: alvinoo
16 Replies

3. Shell Programming and Scripting

Checking the file depending on the input variable

Hi I have a requirement of taking time as input variable outside the script.depending on the time it will check the file output .like ./ <30 min> so should run every 5 minutes ie.6 times to check the output file.Can any one please help here. (7 Replies)
Discussion started by: netdbaind
7 Replies

4. Shell Programming and Scripting

TCSH user input error checking

This was taken down recently because it appeared to be homework, but it isn't. It's for a script I am working on at work. Thanks for the help. How do you check that user inputs (arguments 1 and 2) are both numbers and are at least 5 digits in length? (2 Replies)
Discussion started by: thibodc
2 Replies

5. Shell Programming and Scripting

comparing users input to a file in another directory?

how would i go about comparing a users input to a file which is in another directory? (2 Replies)
Discussion started by: gangsta
2 Replies

6. Shell Programming and Scripting

How to write shell script for input file name format checking?

Hello, I had written a shell script that accepts input file as cmd line argument and process this file. if ; then if ; then . $1 LOGFILE="$LOG_FILE/MIG_BIOS.log"; get_input_file else ERROR_CODE=MSCRM0005_003 error "$ERROR_CODE : Input file $1 is not available"; exit... (3 Replies)
Discussion started by: Poonamol
3 Replies

7. Shell Programming and Scripting

Checking input is Numerical

Hey guys, i was looking for some examples of how can i check if the use input is just using numerical. i came across an example using tr : echo "read this" read this if ]`" ] ; then echo "True - only alpha and numeric" else echo "False - there are NON alpha and numeric stuff here!" fi... (7 Replies)
Discussion started by: gregarion
7 Replies

8. Shell Programming and Scripting

Checking input for being numeric and integers

Hi, I'm trying to check to see that the arguments given to my script are both numeric and positive integers. I'm using tcsh. I figured out the positive part, but I am having trouble with the arguments being numeric and integers I have no idea where to get started with the checking them actually... (1 Reply)
Discussion started by: mistykz
1 Replies

9. UNIX for Dummies Questions & Answers

checking wether an input is using letters of the alphabet

afternoon forums. I need to get a way of testing as to wether an inputed character is part of the english alphabet. i have come up with the following code but its not working at all. until '] do echo This is not a Letter done any help would be beneficial to me. (1 Reply)
Discussion started by: strasner
1 Replies

10. UNIX for Dummies Questions & Answers

Checking last logon for users

Hi, I'm working on a number of AIX pSeries servers which are using LDAP to manage the accounts. I'm trying to find out if there is a command which will tell me the last logon time of a user on a particular server or if they've never logged on to that server. does anyone know if this can be... (1 Reply)
Discussion started by: m223464
1 Replies

Featured Tech Videos