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
#!/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.
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.
#!/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
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...
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.