ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
You seem to be mainly focusing on expr command so I think you should stick with that. Now to get the values into the script from file1-file3 you should use a command you have already gone over in lab like maybe cat or read. Here is an example using cat with pipe and also file redirection using <.
Code:
#!/bin/bash
while read NAME SCORE;do
echo "NAME = $NAME"
echo "SCORE = $SCORE"
done <file1
#Another way to do it would be
echo "______________"
cat file1|
while read NAME SCORE;do
echo "NAME = $NAME"
echo "SCORE = $SCORE"
done
Now using what you aready know you should be able to add the $SCORE increment $COUNT and finally divide $TOTAL by $COUNT if $COUNT != 0.
This is much more like what we are doing in class, thank you. Here is what I typed in, I still can't figure it out, but I am so much closer:
Code:
#!/bin/sh
sum=0
total=0
count=0
while read NAME SCORE;do
sum=`expr $sum + $SCORE`
count=`expr $count + 1`
done <$1
echo "sum = $sum"
echo "count = $count"
if $count != 0
then
echo "average = $sum/$count"
fi
But here is what shows up:
Code:
bash-2.05$ new quiz1
sum = 0
count = 0
./new: 0: not found
So, it isn't adding up the scores or count at all, anyone know why?
Last edited by radmofopunk; 04-22-2006 at 07:18 PM.
The "read" doesn't necessarily like end-of-file; "expr" doesn't like null variables - and one or the other could be messing up your other variables.
Please try something like this:
Code:
#set -x
while read NAME SCORE;do
if [ ! -z "$NAME" ] && [ ! -z "$SCORE" ]; then
SUM=`expr $SUM + $SCORE`
COUNT=`expr $COUNT + 1`
fi
done < $1
This "protects" your expr against null values for NAME or SCORE.
PS:
If you uncommented "set -x", you get very useful debug statements.
PPS:
The purpose of quoting "$NAME" and "$STRING" like that was to insure that the "[ ! -z ]" test command gets an empty string ("") instead of nothing at all ( ).
Definitely good stuff - and, based on your experience with this assignment, I think you'll get a lot more out of it the next time you look at it. IMHO...
Make sure you don't have any extra lines in your quiz file. The extra lines could be causing problems. Here is a way to test it. The output should look like the first example.
$ cat quiz1;echo "end"
Ann 24
James 20
Joseph 22
Melanie 20
Timothy 24
end
$ cat quiz1;echo "end"
Ann 24
James 20
Joseph 22
Melanie 20
Timothy 24
end
If the output looks like the second example then you have extra lines in the quiz1 file and you need to delete them.
The "[ ! -z ... ]" check will protect against blank lines (and/or missing data fields). I think the problem radmofopunk ran into was probably at the end of the file - which could occur even if the quiz file didn't appear to have any blank lines.
In any case: the best policy is always to assume that your input data *will* have errors, and protect against it in your code.
Nice. that != in the if statement means if $count isn't 0 then divide the sum by the count, and if it is 0 then say undefined right? I am unfamiliar with using !=
this is a great lesson we should take care of blank files, and zero division
sorry radmofopunk, i put the wrong filename in my script
it should be quiz1, not jawab
and when you say (in my script)
Code:
for x in $(cat quiz1)
you're reading each word (separated by whitespaces)in the file
and when you do math calculation, it abandons the string words
this is a great lesson we should take care of blank files, and zero division
That script doesn't take care of blank lines or even lines with extra whitespace. And blank files... don't even go there. Try putting an extra space in the data or an extra line at the end or give it a blank file and watch it crash.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.