Thank you for filling out the template. You wouldn't believe how many try to sneak by it.
You've made a solid try but there's a lot of syntax problems you need to clean up. There's also much easier ways to do some of the things you're trying. Also, your logic isn't airtight.
Code:
#!/bin/bash
# Richard Ferguson Unix/Linux CS121
user=0
score=0
count=0
sum=0
highest=0
# set 'smallest' to a real high number, so the first number you read is always smaller!
smallest=99999
# This is wrong. You need spaces.
# Also, use = instead of ==.
# until [[$user=="done"]]
until [[ "$user" = "done" ]]
do
# No point doing this stuff here. Do it ALL inside your 'score' loop.
# echo -n "Enter Score : "
# read score
# This is wrong. Needs spaces. Also, =.
# But you might as well just do an infinite loop.
# until[[$score==999]]
while true
do
echo -n "Enter Score : "
# you forgot the read
read score
# Instantly break the loop if we get >= 999.
# && is a short-form if/then.
# Much easier to read than if [[ "$score" -ge 999 ]] ; then break ; fi
[[ "$score" -ge "999" ]] && break
# Variables don't work that way. Use $(( )) for math.
# sum + = $score
sum=$((sum+score))
# Much easier ways to do this.
# if (( $score > highest ))
# then
# highest = score
# && is a short-form "if". [[ condition ]] && thing-executed-if-statement-is-true
# There's also an || which does [[ condition ]] || thing-executed-if-statement-is-false
[[ "$score" -gt "$highest" ]] && highest="$score"
[[ "$score" -lt "$lowest" ]] && lowest="$score"
# No reason to nest three 'ifs' here. No reason to check for 999 inside
# them either.
# if (( $score < lowest ))
# then
# lowest = score
# if (( $score -ne 999 ))
# then
# $howmany+ = 1
# $sum+ = score
# fi
#fi
#fi
# you forgot every one of your 'done's.
# You should have one to match the end of each while/until/for,
# just like fi's match if's.
done
echo -n "Type 'done' to finish: "
read user
done
# again, math doesn't work this way.
# avg = $sum/$howmany
# You can embed $(()) math in the middle of things, usefully.
# You don't need -n to print more than one thing. Just put more than one thing into echo.
echo "average $((sum/count)) sum $sum highest $highest lowest $lowest"
echo
echo "Goodbye"
exit 0
I don't think this code will do exactly what the assignment wants. I get the feeling they want totals per-loop, not per session, but you should ask your teacher that. I leave fixing the logic to do that up to you.
Last edited by Corona688; 11-15-2011 at 11:25 AM..
i need to prompt the user for
a) group of scores
b) calculate the lowest and highest scores and overall average
c) print out to the screen
d ask if user has any more data to process, if so repeat (loop) for a new set of scores if not exit.
i have this much,and trust me i know... (1 Reply)
I'm taking a class right now and need ideas for a project. The scope is relatively vague: "Create a 'mash-up' program that combines system administration functions into a single application." I would say my skills working in bash are between average and slightly above average. I was originally... (2 Replies)
Hello people, I am new to the forum and to scripting and I'm honored to be a part of the Forum :)
At the moment I'm learning to do basic scripting for school.
Now I got 2 assignments that I do not understand. The case scripting I have mastered a bit. But now I have an assingment to make with... (1 Reply)