Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Shell script question

Posted on 1998-11-21
6
299 Views
Last Modified: 2010-05-18
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 =)      
0
Comment
Question by:markiez
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 2007981
echo $quest | cut  -d' ' -f 1
0
 

Author Comment

by:markiez
ID: 2007982
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 2007983
if ( echo $quest | grep '^will ')
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:arthurd
ID: 2007984
A different way:

if [ `echo $quest | awk '{print $1}'` = "will" ]

This grabs the first field of quest.

0
 

Accepted Solution

by:
bmoore111898 earned 200 total points
ID: 2007985
Here is a complete program for you..

#!/bin/sh

echo "Enter your question: \c"
read question
echo

firstword=`echo $question | cut -d' ' -f1 | tr 'A-Z' 'a-z'`
# The pipe through tr is used to convert to lowercase, so if they type Will,WILL, or will
# it will work

case $firstword in
  "hello"|"hi" )  
                echo "Hi yerself,\c";
                echo "But thats not a question";
                echo "Why don't you try asking one";;
  "will"   )   echo "Certainly not";;
  "who"    ) echo "You";;
   default )  echo "How should I know";;
esac

# In the case statement above notice the use of single semi-colons and double #semi-colons.  With single ones you can have multiple statements for a single case.
#Use a double semi-colon to end that case.
0
 

Author Comment

by:markiez
ID: 2007986
good job! u should get an A, expert.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question