markiez
asked on
Shell script question
I am a begining unix student and our instructor made us write simple scripts. I have this particular problem that i and my teacher couldn't solve at this time:
I am writing a script that works in a similar way as a "Magic 8 Ball", in which the person types in a question and the script generates an answer based on what they type. But actually, the answer is only determined depending on the first word they type in the sentence. For example, the person may write "Will i be rich someday" and the script answers "certainly not". The script seems smart but actually, the answer was determined because he typed the word "will" in the begining of his question. so whatever question they type and it contains a "will" inthe begining , they will get the same "certainly not" answer. And when someone types a "who" in the begining, another answer is designated for that.
The problem is that I have a hard time making the script work in a way that it only grabs the first word of their question and that determines the answer. I tried cut, paste and greps but i was not successful. here is my script:
#thename=`logname`
#echo "~~~~~ WELCOME $thename TO THE MYSTERIOUS BEYOND~~~~~ #\n
#What is your question? \c $quest"
#read quest>file1
# if [$quest = `cat $quest | grep will`]
# then echo "Don't count on it!"
# else echo "please ask again"
i typed in the # signs just to make sure that the script wouldnt affect your system (im not even sure if it will) this is how far i got and it is still wrong. Please help. its driving me crazy! thanks =)
I am writing a script that works in a similar way as a "Magic 8 Ball", in which the person types in a question and the script generates an answer based on what they type. But actually, the answer is only determined depending on the first word they type in the sentence. For example, the person may write "Will i be rich someday" and the script answers "certainly not". The script seems smart but actually, the answer was determined because he typed the word "will" in the begining of his question. so whatever question they type and it contains a "will" inthe begining , they will get the same "certainly not" answer. And when someone types a "who" in the begining, another answer is designated for that.
The problem is that I have a hard time making the script work in a way that it only grabs the first word of their question and that determines the answer. I tried cut, paste and greps but i was not successful. here is my script:
#thename=`logname`
#echo "~~~~~ WELCOME $thename TO THE MYSTERIOUS BEYOND~~~~~ #\n
#What is your question? \c $quest"
#read quest>file1
# if [$quest = `cat $quest | grep will`]
# then echo "Don't count on it!"
# else echo "please ask again"
i typed in the # signs just to make sure that the script wouldnt affect your system (im not even sure if it will) this is how far i got and it is still wrong. Please help. its driving me crazy! thanks =)
echo $quest | cut -d' ' -f 1
ASKER
Thanks ozo for submitting the solution:
echo $quest | cut -d' ' -f 1
I would just like to ask where exactly in the script do i put this line? Thanks
echo $quest | cut -d' ' -f 1
I would just like to ask where exactly in the script do i put this line? Thanks
if ( echo $quest | grep '^will ')
A different way:
if [ `echo $quest | awk '{print $1}'` = "will" ]
This grabs the first field of quest.
if [ `echo $quest | awk '{print $1}'` = "will" ]
This grabs the first field of quest.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
good job! u should get an A, expert.