Visit Our UNIX and Linux User Community


Error Checking Problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Error Checking Problem
# 1  
Old 09-09-2006
Error Checking Problem

Here is my problem

I'm designing a script that will have to accept user input from the command line using the read command.

The problem is that I have to check the input which should be in the format of

<first name> <last name> <phone number (8 digits long)>

The input can also be

Just <first name>
Or just <last name>
Or <phone number>

I need a way to test this input before I pass it to grep with a text file that has stored names and phone numbers i.e

Stephen Smith:12345678

The user should be able to search using the first name, last name or the phone number for example a user types in Stephen, the entry in the text file should display.

The test should include that no numbers are in the first and last names and that the phone number does not start with a 0 (zero). These are just errors checks which will display a message to user if they have entered it in incorrectly.

Any help would be appreciated.

Thanks :-)

Here is some code that I've done

Code:
#!/bin/sh

$entryType

echo "Please search: \c"
read text

case $text in
[a-zA-Z]*[' '][0-9*] entryType='3' ;;
[a-zA-Z]*) entryType='1' ;;
[0-9]*) entryType='2' ;;
*) entryType='4' ;;
esac

if test $entryType -eq 3; then
grep -i $text textfile.txt
fi

*Code is incomplete and just tries to establish what sort of input the user made, so now I just need to test it and if it passes then I can pass it to grep to look it up in the texfile. If its not in the textfile I will then add it.
# 2  
Old 09-09-2006
I've tried to do it with appending the read of the user to a text file and then reading it back in using grep.

Here is the code below, still not complete.

Code:
#!/bin/sh

$entryType

read text
echo $text > output.txt

case $text in
    [a-zA-Z]*[' '][a-zA-Z]*[' '][0-9]*) entryType='3' ;;
    [a-zA-Z]*[' '][a-zA-Z]*) entryType='4' ;;
    [a-zA-Z]*) entryType='1' ;;
    [0-9]*) entryType='2' ;;
    *) entryType='5';;
esac

if test $entryType -eq 1; then
    if egrep "[a-zA-Z]*" output.txt; then
        grep -i $text teledir.txt || echo "No such entry"
    else
        echo "Invaild name"
    fi
fi

if test $entryType -eq 2; then
    if egrep "[1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]" output.txt; then
        grep -i $text teledir.txt || echo "No such entry"
    else
        echo "Invaild phone number"
    fi
fi

if test $entryType -eq 3; then
    if egrep "[a-zA-Z]*[' '][a-zA-Z]*[' '][0-9]*" output.txt; then
        grep -i $text teledir.txt || <write to text file> | echo "New
entry $text added"
    else
        echo "Invaild phone number"
    fi
fi


if test $entryType -eq 4; then
    if egrep "[a-zA-Z]*[' '][a-zA-Z]*" output.txt; then
        grep -i $text teledir.txt || echo "No such entry"
    else
        echo "Invaild name"
    fi
fi

# 3  
Old 09-12-2006
Hi,
Get your input from the read command, then check to make sure you only have A-z and 0-9 then you can safely grep through your file.

try this.....

#!/bin/sh
echo "Enter search string :\c"
read var
if [ -z `echo $var | tr -d "[:alnum:]"` ]
then
grep -i $var filename
else
echo "Invalid characters in your serach string"
echo "try again...."
fi

Basiclly remove all the GOOD characters (A-z,0-9) and if nothing is left the entered search string is OK to be used.

Put it in a loop and you can keep going around until the user gets it right.
# 4  
Old 09-12-2006
hi
is there any reason that you need to check that the names do not contain numbers and stuffs like that??
i could just use grep to search against your telephone directory using the search term, and simply flag out to user "Entry not found" , if it does not match what user types in.
If need be, i can just provide a simple help message telling the user what he can or cannot enter as a search term when the grep fails..
It would be simpler this way..well at least to me...
# 5  
Old 09-13-2006
No you can take the input as is. I read your post as being, "need to check input before passing to grep" Hence that code. (Checking for non printable characters)

Other wise don't bother and just let grep do its thing.

A though just crossed my tired mind... you could use tr to delete any non printable characters first. just a thought.

Previous Thread | Next Thread
Test Your Knowledge in Computers #688
Difficulty: Medium
The first computer to implement architectures enabling reverse Polish notation (RPN) was the Apple I computer.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with ssh and checking if file exists

Hi All, I am facing a problem while checking for existence of file over ssh ! Basically, i want to ssh and check if file exists.. If file exists return 1. If file does not exits return 0 (or any value) I am using the below code file_avail=`ssh username@host "if ]; then exit 1;... (10 Replies)
Discussion started by: galaxy_rocky
10 Replies

2. Shell Programming and Scripting

Problem with a script for checking the state of a process

Hello Everyone, I have a process that should be always running. Unfortunately, this process is getting down almost every 10 minutes. I want to make a script that verify the state of this process: If the process is up, the script shouldn't do nothing and if it's down he should run it. Can... (3 Replies)
Discussion started by: adilyos
3 Replies

3. Shell Programming and Scripting

Error checking help.

I am currently wrapping up a assignment for one of my classes. I need help writing some error checking logic. The problems I am having are: keeping track of the records I have deleted and reported it back using echo. I have no idea how to do this. ensuring that line numbers fall... (1 Reply)
Discussion started by: Boltftw
1 Replies

4. Programming

C++ - Problem in asking and checking user's passwd

This is the source code: #include <pwd.h> #include <iostream> #include <string.h> using namespace std; int main() { struct passwd *user; char login="alex", password="qwertyuiop"; if ((user= getpwnam(login)) == NULL) cout << "No such user\n"; else if... (24 Replies)
Discussion started by: hakermania
24 Replies

5. Programming

Error Checking

Hey guys i am facing a problem in my sql statement. I am trying to check if there is such a value in the database. Code: string NewMovie = "ww"; string queryText ; queryText = "Select * from movie_info WHERE movie_title = '"+ NewTitle +"'"; ... (1 Reply)
Discussion started by: gregarion
1 Replies

6. UNIX for Dummies Questions & Answers

Basic number checking problem

Hello all I am having problems using a bash script to read the input from the user and checking that its a valid number. I only want the user to input a maximum of a 3 number string (321 , 521 , 871 etc.). Anything longer or that includes a chararcter or symbol will display an error message. ... (8 Replies)
Discussion started by: ChrisHoogie
8 Replies

7. Shell Programming and Scripting

Facing problem in checking the directory existence

Hi All, I have a written a scell script which checks the existence of a directory. The dierctory name is read from property file. Though the dierctory exists but it says - it does not exist-- Find my code below -- #! /bin/ksh #Tibco Properties file is inclusion .... (5 Replies)
Discussion started by: supriyabv
5 Replies

8. UNIX for Dummies Questions & Answers

Problem in checking space of mount.Please help me

Hi everyone, I am writing a script. As a part of this script, I wanted to check the space of few mounts, If the space usage percentage of the mount crosses over a certain limit then i wanted to display a warning message. The following is the command df -k | awk ' { if (($6 == "/export/temp")... (4 Replies)
Discussion started by: Sheethal
4 Replies

9. Shell Programming and Scripting

Problem in checking file abort

Hi, I would like to know given executing a file with inputs, I would like to know when does it terminate /abort abnormally. I tried to append an echo $? after executing my program which is in C. However, there is nothing..It shows 0 even though the program actually exit. my command is... (1 Reply)
Discussion started by: ahjiefreak
1 Replies

10. Shell Programming and Scripting

Need help with error checking

I am creating a script that will automatically use sftp to connect to a site and download a file, extract the tar and then delete the tar file once completed. What I am stuck on is the error checking for this process. Here is the code so far: Now this works for me as is, but i need... (0 Replies)
Discussion started by: xianoth
0 Replies

Featured Tech Videos