LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Closed Thread
  Search this Thread
Old 03-03-2018, 12:34 PM   #1
hebruiser
LQ Newbie
 
Registered: Mar 2018
Location: NC
Distribution: CentOS, Mint 18.4, Fedora 27 and Solus
Posts: 6

Rep: Reputation: Disabled
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
 
Old 03-03-2018, 10:28 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,666
Blog Entries: 6

Rep: Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531
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.

Last edited by astrogeek; 03-03-2018 at 10:30 PM. Reason: once
 
1 members found this post helpful.
  


Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to make a WHILE loop iterate over script until user input stops hebruiser Programming 3 03-05-2018 08:31 PM
hang script (trap) for user input in a loop with ctrl and any other letter pedropt Programming 4 12-10-2017 04:06 PM
[SOLVED] A question about for-loop in shell script thomas2004ch Linux - Software 4 10-30-2013 05:12 AM
Shell script user input tuckermaddox Linux - Newbie 5 08-12-2004 03:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration