# simple shell script problem

**URL:** <https://community.unix.com/t/simple-shell-script-problem/158005>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [December 6, 2005, 8:41pm UTC](https://community.unix.com/t/simple-shell-script-problem/158005 "2005-12-06T20:41:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![djt0506](https://community.unix.com/letter_avatar/djt0506/32/5_5575768a8748004e209b776fc1b2916d.png) [@djt0506](https://community.unix.com/u/djt0506)\
**Post date:** [December 6, 2005, 8:41pm UTC](https://community.unix.com/t/simple-shell-script-problem/158005/1 "2005-12-06T20:41:54Z")

</div>

hi all. i have a little problem. im basically reading input from the user from the keyboard into the variable "phonenumber". I want to do a little error check to check if the user doesnt enter anything in for the value phonenumber.

i had this:

read phonenumber

if [$phonenumber=" "]  
then  
.....  
else  
.....  
fi

but thatjust performs then instructions in between "then" and "else", but not "else" and "fi". any ideas?

thanks all in advance for your help.

---

<div class="post-metadata">

**Author:** ![Bhups](https://community.unix.com/letter_avatar/bhups/32/5_5575768a8748004e209b776fc1b2916d.png) [@Bhups](https://community.unix.com/u/Bhups)\
**Post date:** [December 6, 2005, 10:10pm UTC](https://community.unix.com/t/simple-shell-script-problem/158005/2 "2005-12-06T22:10:25Z")

</div>

hiiii djt0506,

got a easy solution for you 🙂 ...

you neeed to write a function "function Func\_validate\_phone\_number" which will validate the phone number entered by the user.  
if the ph no is blank it will again call the same function and will ask the user to enter it again....until he enters any thing other than "enter".

if you want to validate it further (for valid numbers or no characters etc) then you need to do some more validations....which again quiet easy :p.

here is my code....

function Func\_validate\_phone\_number {

if [-z "$ph"]  
then  
echo "Phone Number cannot be blank....please re-enter."  
read ph  
Func\_validate\_phone\_number "$ph"  
else  
echo "Thanks for giving your phone number.....bye"  
fi

}

echo "Enter the phone number"  
read ph  
Func\_validate\_phone\_number "$ph"

i have tested it .....it works fine 😉 ...

cya

---

<div class="post-metadata">

**Author:** ![blowtorch](https://community.unix.com/user_avatar/community.unix.com/blowtorch/32/136_2.png) [@blowtorch](https://community.unix.com/u/blowtorch)\
**Post date:** [December 6, 2005, 11:00pm UTC](https://community.unix.com/t/simple-shell-script-problem/158005/3 "2005-12-06T23:00:23Z")

</div>

Keep the code short and simple. Bhups, you have a nice idea there, but I think it may be confusing for some (recursive code can be).

```nohighlight
#!/usr/bin/sh
echo "Enter phone number: \c"
while read phone_no; do
        if [-n "$phone_no"]; then
                break
        fi
        echo "No input offered. Please enter phone number: \c"
done

```
