LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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-22-2006, 07:03 PM   #16
radmofopunk
LQ Newbie
 
Registered: Apr 2006
Posts: 10

Original Poster
Rep: Reputation: 0

Quote:
Originally Posted by /bin/bash
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.
 
Old 04-22-2006, 08:13 PM   #17
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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 ( ).

PPS:
A long time and many posts ago, Dive gave you a useful link to a good shell scripting site:
http://www.tldp.org/LDP/abs/html/

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...

Last edited by paulsm4; 04-22-2006 at 08:19 PM.
 
Old 04-22-2006, 08:38 PM   #18
radmofopunk
LQ Newbie
 
Registered: Apr 2006
Posts: 10

Original Poster
Rep: Reputation: 0
Same results, still sum=0 and count=0

It is adding it up correctly, according to the debugging thing, but it resets itself back to 0 for some reason
 
Old 04-22-2006, 10:05 PM   #19
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Here's the data set I tested:
Quote:
cat quiz1.txt
Ann 24
James 20
Joseph 22
Melanie 20
Timothy 24
Here's the complete script:
Code:
#!/bin/bash

  # Chk cmd-line args
  if  [ $# -ne 1 ]; then
    echo "USAGE: chk_grades <filename>"
    exit 1
  fi

  SUM=0
  TOTAL=0
  COUNT=0
  SCORE=0

  #set -x
  while read NAME SCORE;do
    if [ ! -z "$NAME" ] && [ ! -z $SCORE ]; then
      SUM=`expr $SUM + $SCORE`
      COUNT=`expr $COUNT + 1`
    fi
  done < $1

  echo "SUM = $SUM"
  echo "COUNT = $COUNT"
  if [ $COUNT -gt 0 ]; then
    AVG=`expr $SUM / $COUNT`
    echo "average = $AVG"
  else
    echo "AVERAGE UNDEFINED!"
  fi
And here's sample output:
Quote:
./grade_quiz.sh quiz1.txt
SUM = 110
COUNT = 5
average = 22
 
Old 04-23-2006, 01:25 AM   #20
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
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.
 
Old 04-23-2006, 02:18 AM   #21
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi, /bin/bash -

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.

IMHO .. pSM
 
Old 04-23-2006, 06:24 PM   #22
radmofopunk
LQ Newbie
 
Registered: Apr 2006
Posts: 10

Original Poster
Rep: Reputation: 0
Well, I figured it out. It wasn't a problem with the quiz1 having blank lines, here is the final code that works:

Code:
bash-2.05$ cat average
#!/bin/sh
sum=0
count=0
exec <$1
while read line
do
        set $line
        sum=`expr $sum + $2`
        count=`expr $count + 1`
done
echo "sum = $sum"
echo "count = $count"
if [ $count != 0 ]
        then
                echo "average = `expr $sum / $count`"
        else
                echo "average = undefined"
fi

exit 0
 
Old 04-23-2006, 06:45 PM   #23
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Cool - excellent work, radmofopunk!
 
Old 04-23-2006, 07:58 PM   #24
radmofopunk
LQ Newbie
 
Registered: Apr 2006
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks again to everyone, couldn't have done it without you.
 
Old 04-23-2006, 08:53 PM   #25
cramer
Member
 
Registered: Feb 2006
Distribution: Red Hat 9
Posts: 112

Rep: Reputation: 15
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 !=
 
Old 04-23-2006, 10:09 PM   #26
radmofopunk
LQ Newbie
 
Registered: Apr 2006
Posts: 10

Original Poster
Rep: Reputation: 0
Yes, != means not equal, so if it isn't 0, then it will divide it, otherwise (or it is 0), it will display undefined.
 
Old 04-23-2006, 10:53 PM   #27
JrLz
Member
 
Registered: Mar 2004
Location: Jakarta
Posts: 164

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

it wasn't a good script, just forget it
 
Old 04-24-2006, 03:13 AM   #28
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

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


Reply



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

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

All times are GMT -5. The time now is 09:25 PM.

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
Open Source Consulting | Domain Registration