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.
I am trying to do this lab for school and I can not figure out what to do from here. Here are the directions:
1. Create a file called quiz1 with the following data:
Ann 24
James 20
Joseph 22
Melanie 20
Timothy 24
(The names are in one column, and the numbers are in another)
2. Create a similar file called quiz2, and also create a file called quiz3 that has no information in it.
I have done all of this, that was the easy part. Now here is where I am stuck:
3. Create a script called average. The objective of average is to read in the scores from a file like quiz1 and report the sum of the scores, the number of the scores, and the average score. For exampple, the command ./average quiz1 should produce the following output:
sum = 110
count = 5
average = 22
After creating average, run it on quiz1, quiz2, and quiz3. Show the output below.
(Note, your script should not try to divide the sum by the count if the count is zero. So, if the count is zero, report "average = undefined".)
I don't even know where to start.. How do I get it to read the numbers from the 2nd column but not the names from the first? This is probably a really simple script for you guys, can someone make it for me so I can learn from it? It's due Monday, I've been struggling on this for a week. I have nothing. Please help, thanks.
Well, like I said.. I don't even know where to start. I don't know how I would get it to read the second column of numbers, and ignore the names.. because you have to add up the numbers, not the names. Any idea of how I would do that part?
#!/bin/bash -e
total=0
count=0
for x in $(sed 's/^.* //' $1)
do
total=$(($total+$x))
((count++))
done
echo "Total=$total"
echo "Number of People=$count"
echo "The average is $(echo $total/$count | bc)"
Save it as test.bash and pass it the name of the file with the results in as an argument like this
Code:
test.bash quiz1
I haven't started to think about the test for zero yet. would the command `test' be the one to use?
FYI without knowing what the OPs has learned in the lab or class so your code might use commands that have not been taught yet. The instructor will know that it isn't original work. Have you (OPs) asked the lab instructor or your fellow classmates for help yet?
Andrew, I typed in the script just like you said, but I saved it as test rather than test.bash, and when I typed in test quiz1, nothing showed up at all! I'll get a screen shot of it and show you, I have to double post though because I can't post links until I have 3 posts, sorry guys.
And michaelk you brought up a good point, that script isn't like ones we have been doing, must more advanced. The whole "sed" thing, I've never heard of it. Let me show you a few screenshots of my most recent lab, maybe you can get an idea of what I am trying to get at for this lab, and what kind of commands to use:
Thanks again guys, I am trying to get through this. I can't ask the teacher, the lab is due Monday, and I don't know anyone in class enough to ask them. Thought I would come to you guys, I'd trust you guys more than the person sitting next to me anyways.
Andrew's script's fine in my place
here if you may use awk, but I guess it won't since you're not using sed either...
Code:
awk 'BEGIN {sum=0;count=0;average=0}{sum=sum+$2;count=count+1} END {print "Sum="sum "\nCount="count "\nAverage="sum/count}' quiz1
here if you would try mine
Code:
#!/bin/bash
total=0
count=0
for x in $(cat jawab);
do
for y in $x;
do
continue
done
let total=total+$y
let count=count+1
done
count=$[$count/2]
echo "sum=$total"
echo "count=$count"
echo "average=$[$total/$count]"
the count is added by one 2x in each line, I don't know how to fix it
Avoid saving files as test because test is also a command used by linux, I.E. when you type in
test quiz1 bash
Linux thinks you want to use the test command on the file quiz1, when really you want to run the script on the file quiz1. Therefore, although it seems right to name a script as test, you should avoid the name, give it a name like lab3.sh or avgscore.sh and then use
./avgscore.sh quiz1
To run the script on the file quiz1. That would be why the script wasn't showing up anything. The test command returns values as true or false, and is used mostly in the if statements of bash scripts, those are the lines of code that look like this:
if [ "you" = "joe" ]; then
The stuff in between [ and ] is used by the test command to see if it is true and then it acts accordingly, for example here, if the value "you" is equal to the value "joe" then the script should do something, otherwise it should do something else.
cramer, thanks for the heads up about naming things test. That makes sense now. Still a no go though for both of those! This is really confusing stuff.
Here are screen shots for the first and second scripts..
Maybe someone can try to explain to me what each step is doing, and I can try to figure a way out to do it too. Think simple guys, this is just for a 100 level class, and should only require basic stuff. I think we are supposed to "read" from the file quiz1, some how get the numbers from the second column, and add those up. Maybe run a loop while reading from that file, or something. Any ideas why these aren't working?
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.
Awk, of course, that's much better than sed. Thanks JrLz. I'm more familiar with sed so that was my instinctive choice. Awk copes much better if it's fed an empty file to process. That one line of awk is cool. You might want to use bc to do the maths though as it can handle decimals (adjust the acuracy with the scale argument). I found a simple `case' was the easiest way to test if the count is zero.
Code:
#!/bin/bash -e
total=0
count=0
for x in $(awk '{print $2}' $1)
do
total=$(($total+$x))
((count++))
done
echo "Total=$total"
echo "Number of People=$count"
case $count in
0)
echo "average = undefined" && exit 1
;;
*)
echo "The average is $(echo "scale=3; $total/$count" | bc)"
;;
esac
radmofopunk, you need to learn to type, or copy and paste from your browser.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.