LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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


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

Rep: Reputation: Disabled
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
 
Old 03-03-2018, 01:19 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro
Posts: 6,657

Rep: Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336
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

Last edited by BW-userx; 03-03-2018 at 01:29 PM.
 
1 members found this post helpful.
Old 03-03-2018, 06:41 PM   #3
hebruiser
LQ Newbie
 
Registered: Mar 2018
Location: NC
Distribution: CentOS, Mint 18.4, Fedora 27 and Solus
Posts: 6

Original Poster
Rep: Reputation: Disabled
Derrrr...thank you. You rule.
 
Old 03-05-2018, 08:31 PM   #4
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 876

Rep: Reputation: 379Reputation: 379Reputation: 379Reputation: 379
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
 
1 members found this post helpful.
  


Reply


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
Newbie Question - Shell Script While Loop Based on User-Input hebruiser Programming 1 03-03-2018 10:28 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
for loop to iterate four times, print , then start again casperdaghost Programming 1 07-24-2011 02:19 PM
[SOLVED] Bash Script Problem - Stops after While loop K_REY_C Linux - Newbie 7 05-13-2010 06:59 PM
How do I make my BASH script yes/no user input query terminate with 3 invalid inputs? lupusarcanus Linux - Newbie 9 03-16-2010 03:43 PM

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

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

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