LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to make a WHILE loop iterate over script until user input stops (https://www.linuxquestions.org/questions/programming-9/how-to-make-a-while-loop-iterate-over-script-until-user-input-stops-4175624869/)

hebruiser 03-03-2018 01:01 PM

How to make a WHILE loop iterate over script until user input stops
 
howdy,
Total newb here. I am learning bash scripting and am in the dark about this one. I need the while loop to prompt the user AFTER the initial running of the script, if they want to do it again or not. If so, run the script again and if not, display a simple message stating script halted by user and then exit.

Here's what I have and it works accept for the stupid while loop which I am not sure where to put...top or bottom.

Any helpful nudges or tips would be great. If you feel like explaining all of this, even better! I have a degree in English, which means I've been a bartender for 20 years.

Code:

#!/bin/bash
# Bash shell script to calculate student average
# Usage: ./grade1.sh   

# Declare some integer type variables
echo -n "Do you wish to enter another set of scores? (y/n) "
read CHOICE
while [ "$CHOICE" != "n" ]
do
       
declare -i test1
declare -i test2
declare -i test3
declare -i test4
declare -i lab
declare -i sum

    echo
    echo "=================="
    echo "Grade Calculator  "
    echo "=================="
    echo
    read -p "Enter first name: " firstname
    read -p "Enter last name: " lastname
    echo
    read -p "Enter test score 1: " test1
    read -p "Enter test score 2: " test2
    read -p "Enter test score 3: " test3
    read -p "Enter test score 4: " test4
    read -p "Enter lab score: " lab

    sum=$test1+$test2+$test3+$test4+$lab
    average=$((sum/5))

    if [ $average -ge 90 ]; then
        grade="A";
    elif [ $average -ge 80 ]; then
        grade="B";
    elif [ $average -ge 70 ]; then
        grade="C";
    elif [ $average -ge 60 ]; then
        grade="D"
    elif [ $average -le 60 ]; then
        grade="F"
    fi
    echo "Course Grade: "$grade
    echo
    echo "Grade results . . ."
    echo "Student name: $firstname $lastname"
    echo "Total points: $sum"
    echo "Course average: $average"
    echo

    case $grade in
    A) echo "An 'A' represents superior course work."
        ;;
    B) echo "A 'B' represents above average course work."
        ;;
    C) echo "A 'C' represents average course work."
        ;;
    D) echo "A 'D' represents below average course work."
        ;;
    F) echo "An 'F' represents failing course work."
        ;;
    esac
done


BW-userx 03-03-2018 01:19 PM

http://tldp.org/LDP/Bash-Beginners-G...ect_09_05.html

your read is in the wrong place.
can be written like this, when you've discovered where to place it.
(check link above)
Code:

read -p "Do you wish to enter another set of scores? (y/n) " CHOICE

hebruiser 03-03-2018 06:41 PM

Derrrr...thank you. You rule.

MadeInGermany 03-05-2018 08:31 PM

If you really want the "continue" question at the very beginning (and between subsequent cycles), then you can include it between while and do
Code:

while
  echo -n "Do you wish to enter another set of scores? (y/n) "
  read CHOICE
  [ "$CHOICE" != "n" ]
do
  ...
done

(The last exit code before the do decides if the loop continues.)
But maybe you do not want it at the very beginning?
Then it should be a while true and a late question with a conditional break
Code:

while :
do
  ...
  echo -n "Do you wish to enter another set of scores? (y/n) "
  read CHOICE
  [ "$CHOICE" = "n" ] && break
done



All times are GMT -5. The time now is 01:18 PM.