1

I am trying to write a script that will continue to prompt a user to enter a grade, until the user enters 999.

If the user enters 90 or more, echo A. If the user enters 80 or more, echo B. If the user enters 70 or more, echo C. Otherwise echo Failed.

I am thinking this needs a WHILE script. Below is my current code

#!/bin/bash
# Asuume that the first paramter is going to be a grade
if [ $# -lt 1 ];
then
    echo -n "Please  pass a a grade "
fi
gr=$1
if [ $gr -ge 90 ]
then
    echo "A"
elif [ $gr -ge 80 ]
then
    echo "B"
elif [ $gr -ge 70 ]
then
    echo "C"
else
    echo "Failed"
fi
exit 0

The script prompts me, but closes after I enter 1 grade.

Thanks in advance

4
  • 2
    I am curious that you found a web site to show you how to do 'if' statements, but could not find any that list 'while loops'?? Commented Apr 5, 2018 at 6:41
  • I wasn't sure if I should use a 'do while' or a 'while do'. I tried both methods but couldn't get the code working. I'm new to programming, so I apologize for the noob question but only posted it once I exhausted myself with attempting to get the loops correct Commented Apr 5, 2018 at 10:34
  • And that's cool, we all have to start somewhere, but in future, include your attempt as it may only be a small tweak to your existing work which you would follow better than an outright solution which may be too advanced for you. It also gives responders an idea of what you are thinking ;) Commented Apr 5, 2018 at 12:09
  • Very valid point. I should have posted my loop rather than cleaning up my code before posting Commented Apr 5, 2018 at 18:39

3 Answers 3

1

As you want to prompt the user to input grades until he enters 999, you should use read instead of command line arguments. As bash doesn't have a do-while loop, we can emulate it's behavior using while as shown below:

#!/bin/bash
read -p "Please  pass a grade " gr
while [ $gr -ne 999 ]; do
        if [ $gr -ge 90 ]
        then
            echo "A"
        elif [ $gr -ge 80 ]
        then
            echo "B"
        elif [ $gr -ge 70 ]
        then
            echo "C"
        else
            echo "Failed"
        fi
        read -p "Please  pass a grade " gr
done
exit 0
Sign up to request clarification or add additional context in comments.

2 Comments

Conventionally, exit codes are used for troubleshooting. exit 0 denotes successful execution.
Yes, and what does the script do, if you don't write exit 0?
1

That's not a prompt, it's just a message.

Prompting would be:

read -p "Please pass a grade: " gr

But this get's into conflict with your following gr=$1, so put this into an else block:

if [ $# -lt 1 ]
then
    read -p "Please pass a grade: " gr
else
    gr=$1
fi

Note that you don't need a semicolon at the line end; it's the line break, which can be substituted by the semicolon.

And you don't need an exit to end a script, if you don't want to exit prematurely.

Comments

0

Please try below code.

 while  true
     do 
       read -p "Please  pass a grade:" gr
       if [ ${gr} -eq 999 ]
          then 
              exit 0
       elif [ ${gr} -ge 90 ]
          then
            echo "A"
       elif [ ${gr} -ge 80 ]
            then
                echo "B"
       elif [ ${gr} -ge 70 ]
           then
                echo "C"
            else
                echo "Failed"
       fi
    done 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.