Howdy People :)

I'm a newbie & its my first question here. I've started learning Unix Bourne Shell scripting recently and struggling already :p Can someone PLEASE help me with the following problem. Somehow my script is not working.

* Display an initial prompt of the form:
Welcome to machine name. What is your name?
where machine name is the network name for the machine that the script is running on.
* Read the name that the user enters
* Respond with the output:
Hello name, how are you?
* Read the response from the user
* If the response contains the word “good” respond with:
I’m glad to hear that.
* Otherwise, respond with:
I’m sorry to hear that. Hope you feel better soon.
* Finally output: Good-bye.


Much appreciated guys. Thanks heaps in advance :)

echo -n "Welcome to `hostname`. What is your name? "
read LINE
echo -n "Hello ${LINE}, how are you? "
read LINE
if test "${LINE}" = "good"
then
   echo "I'm glad to hear that."
else
   echo "I'm sorry to hear that. Hope you feel better soon"
fi
echo "Good-bye."

echo -n "Welcome to 'hostname'. What is your name? "

The -n option to echo is not universal. use printf instead:

printf "Welcome to `hostname`. What is your name? "

read LINE
echo -n "Hello ${LINE}, how are you? "
read LINE
if test "${LINE}" = "good"

That will only succeed if the only thing on the line is the word good. To test for what the OP wants, use case:

case $LINE in
     *good*)  echo "I'm glad to hear that." ;;
     *) echo "I'm sorry to hear that. Hope you feel better soon" ;;
esac

then
echo "I'm glad to hear that."
else
echo "I'm sorry to hear that. Hope you feel better soon"
fi
echo "Good-bye."

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.