Results 1 to 8 of 8

Thread: Shell Programming Question

  1. #1
    Join Date
    Apr 2009
    Beans
    3

    Shell Programming Question

    Hi All - I'm new to the forums and new at shell programming, so I have a question about a program I somewhat copied out of a book and would like to manipulate - Here's what I have so far -

    #!/bin/sh

    echo "Is it morning? Please answer yes or no"
    read timeofday
    if [ "$timeofday" = "yes" ]
    then
    echo "Good Morning"
    elif [ "timeofday" = "no" ]; then
    echo "Good Afternoon"
    else
    echo "Sorry - $timeofday is not recognized. Enter yes or no."
    exit 1
    fi
    exit 0

    If you enter anything other than "yes or no", my program exits with "Sorry - $timeofday is not recognized. Enter yes or no.", which is good! Do any of you know of a way to get my program to loop back to the first line where it asks, "Is it morning? Please answer yes or no" if you enter anything other than "yes" or "no" instead of the program just exiting out?

    If I enter, "I'm a noob programmer", I want the program to keep repeating itself until I enter either "yes" or "no".

    Thank you in advance!

  2. #2
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Shell Programming Question

    use a loop. check the bash link in my sig to learn about loops.

  3. #3
    Join Date
    Dec 2008
    Location
    Kennewick, Washington
    Beans
    168
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Shell Programming Question

    Like ghostdog74 said you should use a loop.
    But i would also recommend that you use a case statement.
    This way your program also wont exit after a string other than yes or no is entered.
    Avoid the Gates of Hell. Use Linux.

  4. #4
    Join Date
    Dec 2008
    Location
    Kennewick, Washington
    Beans
    168
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Shell Programming Question

    For a CASE statement you could do the following.
    NOTE: Wrote this off the top of my head. Excuse any possible errors

    Code:
    #!/bin/bash
    echo "Is it morning? Please answer yes or no"
    read timeofday
    case $timeofday in
    	"yes")
    		echo "Good Morning"
    		;;
    	"no")
    		echo "Good Afternoon"
    		;;
    	*)
    		echo "Sorry - $timeofday is not recognized. Enter yes or no."
    		./thisscriptsname.sh
    		;;
    esac
    exit 0
    Avoid the Gates of Hell. Use Linux.

  5. #5
    Join Date
    Aug 2005
    Location
    Fargo, ND, USA
    Beans
    1,499
    Distro
    Kubuntu 10.04 Lucid Lynx

    Re: Shell Programming Question

    It's perfectly fine to use a case statement, but if you're using Bash, there is a special select statement for this purpose.

    Code:
    #!/bin/bash
    
    PS3="Please enter a number: "
    select resp in yes no; do
        if [ "$resp" = "yes" ] ; then
    	echo yes
    	break
        elif [ "$resp" = "no" ] ; then
    	echo no
    	break
        fi
    done
    Help yourself: Search the community docs or try other resources.
    Quote Originally Posted by Henry Spencer
    Those who do not understand Unix are condemned to reinvent it, poorly.
    Let science use your computer when you aren't: Folding@Home.

  6. #6
    Join Date
    Apr 2009
    Beans
    3

    Re: Shell Programming Question

    Thank you for the suggestions!

  7. #7
    Join Date
    Apr 2009
    Beans
    3

    Re: Shell Programming Question

    Thank guys - Here's what I did -

    echo "Sorry - $timeofday is not recognized. Enter yes or no."
    ./myscriptsname
    fi
    exit 0


    It worked!

  8. #8
    Join Date
    Dec 2008
    Location
    Kennewick, Washington
    Beans
    168
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Shell Programming Question

    jpkotta:
    Im glad you pointed that out to me
    I was unaware of that. Thank you!
    Avoid the Gates of Hell. Use Linux.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •