Results 1 to 4 of 4
im witing a phonebook script and ive come to a deadlock i cant get the script to work properly
could someone please look at my script and help me
the ...
- 09-03-2009 #1Just Joined!
- Join Date
- Sep 2009
- Posts
- 2
my phone book script please help
im witing a phonebook script and ive come to a deadlock i cant get the script to work properly
could someone please look at my script and help me
the rules for the script are it must not have a zero as first digit in the telephone number and the tele number must be 8 digits long it must not have a number with a name example joe blogs1 the script must use positonal parameters to run example ./phone.sh "joe blogs 12345678
other examples of how the script must accept to run are
./phone.sh smith
./phone.sh "john smith"
./phone.sh 12345678
./phone.sh "joe blogs" 12345678
Examples of input the script must stop
./phone.sh "joe blogs1"
./phone.sh 09876543
./phone.sh 12345 only allow 8 digits so less than or more thar 8 is not allowed
./phone.sh "joe blogs" 09876543
If i run the script with ./phone.sh "joe blogs" 0987654 for example it should not allow the data to be added to the teledir.txt because the number it not allowed to start with a zero but it is how can i fix the script so it enters only valid data to the teledir.txt
Code:#! /bin/sh # The clear command simply clears the terminal so the screen is easy to read for the user clear # This code checks to make sure the teledir.txt file is there if [ -e teledir.txt ] then echo "\n" else echo "teledir.txt file does not exist script exiting"; exit fi # This is the code to validate if the user has entered 8 digits but no leading zero # And to make sure that a valid name can contain only letters and spaces while [ $# -gt 0 ]; do case "$1" in [A-Za-z]*[0-9]*) echo "Invalid name: Numbers cannot be used with name data"; exit;; [0-9]*[A-Za-z]*) echo "Invalid number:"; exit ;; [1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]) ;; #echo "Number: $1";; [A-Za-z]*) ;; #echo "Name: $1";; *) echo "Invalid input Number can only be 8 digits and NO zero"; exit ;; esac grep -i "$1" teledir.txt >/dev/null if [ $? -eq 0 ] then grep -i "$*" teledir.txt; exit else echo "\n\n The data entered is NOT there \""$*"\" Added to the directory\n\n" echo "$*" >> teledir.txt fi done exit
if anyone can help me with the script i would really aprecialt the help im really enjoying learning scripting but this problem has stumped me
- 09-03-2009 #2
For the future, please put code inside code tags, it is much easier to read.
You haven't told us what your problem is, so until you do we can only speculate.
- 09-04-2009 #3
You say you get a deadlock. What do you mean? Do you get any output at all? What input do you give to cause the lock? Does the script work for other inputs?
I see one bug in your program, based on the way you're using the while loop (hint: your program doesn't need a while loop, as I understand it).
A common debugging technique is to add print statements (or "echo", in the case of Bash scripting) to various points in the file to see how far the script is actually getting. This way, you can pinpoint the line that is causing the problem, and know what to correct.
Anyway, give us some information and tell us what you have already done to try to debug, and it will be much easier for us to help you.DISTRO=Arch
Registered Linux User #388732
- 09-04-2009 #4Just Joined!
- Join Date
- Sep 2009
- Posts
- 2
The script is supposed to be run by providing eather one or two positional paramiters a name, a number, or a name and number. but the script
should not allow a zero as the first digit in the telephone number or a number with a name
the script correcly runs if i type:
./phone.sh 09876543 #Zero not allowed as first digit script stoped correctly
./phone.sh "john smith" #data entered corectly because it was not in teledir.txt
./phone.sh 12345678 # data shown because it was in the teledir.txt
./phone.sh "joe blogs" 12345678
./phone.sh "joe blogs1" #a number not allowed with a name script corectly stoped this
Example of where the script fails is:
./phone.sh "joe blogs" 09876543 #script should of stoped because the number has a zero as first digit
so to sum up the script should be run using positional paramiters
it should not allow a number with a name and it should not allow a zero as first digit and the telephone number can only be 8 digits long.


Reply With Quote
