LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Newbie Question - Shell Script While Loop Based on User-Input (https://www.linuxquestions.org/questions/programming-9/newbie-question-shell-script-while-loop-based-on-user-input-4175624868/)

hebruiser 03-03-2018 12:34 PM

Newbie Question - Shell Script While Loop Based on User-Input
 
I'm trying to get this script to include a while loop that queries the user to choose if they want to run it again or not. I'm not sure what the while loop construct should be or where in the script to put it.

Here's the meat of the program:

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


astrogeek 03-03-2018 10:28 PM

Please post your thread only once, in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate of this one.


All times are GMT -5. The time now is 12:21 AM.