LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 04-21-2006, 12:04 PM   #1
radmofopunk
LQ Newbie
 
Registered: Apr 2006
Posts: 10

Rep: Reputation: 0
Bourne Shell Programming help!


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.
 
Old 04-21-2006, 12:32 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 11,891

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Per the LQ rules,
Do not expect LQ members to do your homework.

Last edited by michaelk; 04-21-2006 at 12:33 PM.
 
Old 04-21-2006, 12:38 PM   #3
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

More to the point, please post some code samples of what you've done so far, and exactly where you're "stuck".

We can't *DO* your homework for you - but we'd be happy to help you get over particular stumbling blocks you might encounter.
 
Old 04-21-2006, 01:02 PM   #4
radmofopunk
LQ Newbie
 
Registered: Apr 2006
Posts: 10

Original Poster
Rep: Reputation: 0
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?
 
Old 04-21-2006, 01:21 PM   #5
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

Rep: Reputation: 64
Perhaps with sed?
Code:
sed 's/^.* //' quiz1
 
Old 04-21-2006, 01:29 PM   #6
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,211

Rep: Reputation: 292Reputation: 292Reputation: 292
http://www.tldp.org/LDP/abs/html/ this may help
 
Old 04-21-2006, 01:41 PM   #7
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

Rep: Reputation: 64
Here's a bit more
Code:
#!/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?
 
Old 04-21-2006, 02:23 PM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 11,891

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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?
 
Old 04-21-2006, 08:00 PM   #9
radmofopunk
LQ Newbie
 
Registered: Apr 2006
Posts: 10

Original Poster
Rep: Reputation: 0
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.
 
Old 04-21-2006, 08:01 PM   #10
radmofopunk
LQ Newbie
 
Registered: Apr 2006
Posts: 10

Original Poster
Rep: Reputation: 0
So here is a link to the screen shot from typing in Andrew's script, and showing how nothing shows up after I type in the command to run it:

http://img185.imageshack.us/img185/7608/lab238tu.jpg

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:

http://img200.imageshack.us/img200/7380/lab22a1li.jpg
http://img200.imageshack.us/img200/8192/lab22b8ql.jpg
http://img200.imageshack.us/img200/1813/lab22c5zz.jpg (this one being the most related to the current lab)

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.
 
Old 04-21-2006, 11:54 PM   #11
JrLz
Member
 
Registered: Mar 2004
Location: Jakarta
Posts: 164

Rep: Reputation: 30
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
 
Old 04-22-2006, 12:29 AM   #12
cramer
Member
 
Registered: Feb 2006
Distribution: Red Hat 9
Posts: 112

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

Last edited by cramer; 04-22-2006 at 12:32 AM.
 
Old 04-22-2006, 12:44 AM   #13
radmofopunk
LQ Newbie
 
Registered: Apr 2006
Posts: 10

Original Poster
Rep: Reputation: 0
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..

http://img186.imageshack.us/img186/9159/lab23x9on.jpg (Andrew's version)

http://img186.imageshack.us/img186/240/lab23y9vm.jpg (JrLz's version)

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?
 
Old 04-22-2006, 04:36 AM   #14
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
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.
 
Old 04-22-2006, 05:58 AM   #15
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

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


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
'sh' shell - Actually calls legacy Bourne shell, or uses system default? Dtsazza Linux - Software 1 10-28-2005 09:20 AM
No Bourne Again Shell or no such directory found apeman Slackware 1 05-29-2004 05:15 AM
Bash Shell vs Bourne infamous41md Linux - Newbie 14 04-11-2003 10:16 PM
Bourne Again SHell help MagInnovision Linux - Newbie 2 02-04-2003 03:43 PM
Bourne shell programming noodle123 Programming 3 04-16-2002 03:46 AM


All times are GMT -5. The time now is 08:57 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration