major help needed phone dir script need advancing

dam i stuck i am trying to write a script that is a basic phone book so i can enter a name or number or both into the scripts and by

using positional paramiters the script will check if the name has a number in it and warn that numbers are not allowed with names, if i

enter a number then the script will check to see if the number has 8 numbers and the first number is not a zero.

this is a example of how i want to run my script
names entered here are for example only

./dir.sh "john smith"
./dir.sh "john smith" 12345678
./dir.sh 87654763
./dir.sh adam

so this is NOT allowed in my script

./dir.sh "john smith1"
./dir.sh 01234567

so as you can see i need to tell the positional paramiters how to filter the user input using $1 $2 any help with this would be very

apreciated.

My broken code so far:

#! /bin/sh

clear

# This code checks to make sure the book.txt file is there and can be read/writen to
if [ -f book.txt ] && [ -r book.txt ] && [ -w book.txt ]

then
echo "\n"
else
echo "book.txt file does not exist script exiting"; exit
fi

# Here is the code to check the name entered is only letters no numbers
name= $1
case $1 in
*) [ ! a-zA-Z ];; *) echo "INVALID NAME";;
*) echo OK ;;
esac

# This code checks to make sure only 8 numbers are used and that the first number is not a zero
case $2 in
[0-9]*) if [ `echo $2 | wc -c` -ne 9 -o `echo $2 | cut -c1` -eq 0 ]; then
echo "No zero as the first number and length must be 8 numbers long"
else
echo yes
fi
;;
*)
echo usage
;;
esac

What's the question?

You have been shown how to check that the name doesn't contain numbers (although you have mangled it in your script -- go back and copy it exactly).

To find the length of a variable use ${#var}.

I don't know why you want to limit the length of a phone number to 8 digitrs; mine has 10, more if you include the country code.

I don't know why you want to limit the length of a phone number to 8 digitrs; mine has 10, more if you include the country code.

well i need 8 digits so that is why im making the code to limit to 8 and zero is not alowed to be in the first digit ok

---------- Post updated at 07:27 PM ---------- Previous update was at 07:24 PM ----------

i need only 8 digits that is why im making the code to limit to 8 and the first digit must not be a zero ok

So what's your question?

the code to verify name and number are clashing if for example i start my script with ./book.sh 12345678 then the code to check if the number is 8 digits long and no zero as first number wont work as that is set to the second positional paramiter.

so i need a way to set the script to use both verifiers no mater if i use one or two inputs to start the script, so i guess im asking how do i mix my positional paramiters :wink: example

./book.sh "john smith"
./book.sh "john smith" 12345678
./book.sh 87654763
./book.sh adam

so this is NOT allowed in my script

./book.sh "john smith1"
./book.sh 01234567

Hi.

You can use a case statement for this

while [ $# -gt 0 ]; do
  case "$1" in
    [A-Za-z]*[0-9]*) echo "Invalid name: $1";;
    [0-9]*[A-Za-z]*) echo "Invalid number: $1";;
    [1-9]*) echo "Number: $1";;
    [A-Za-z]*) echo "Name: $1";;
    *) echo "Invalid input: $1"
  esac
  shift
done

As you already know how to check if the number is 8 digits, I'll leave that to you

thank you very much for the code scottn that helped me very much its perfect for my script :b: