LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash bc (standard_in) 1: parse error (https://www.linuxquestions.org/questions/programming-9/bash-bc-standard_in-1-parse-error-509781/)

hta1984 12-12-2006 01:59 PM

bash bc (standard_in) 1: parse error
 
A line in bash code:
average=$(echo "scale=4; $s/$i" | bc -l)

s=42.91 and i=148,i expect to get average=0,2899.can you fix the error.

Thanks in advance

Hko 12-12-2006 02:58 PM

Tried it:
Code:

#!/bin/bash
s=42.91
i=148
average=$(echo "scale=4; $s/$i" | bc -l)
echo "s = $s"
echo "i = $i"
echo "average = $average"
exit 0

... works just fine:
Code:

chmod +x avg.sh
./avg.sh
s = 42.91
i = 148
average = .2899


hta1984 12-12-2006 05:25 PM

Actually the message i posted was just one line of my code.I thought,only that line was the reason of error.Now,i think something other than that line casues error.I'm sending the full code.

Note:The aim of this code is to analize output of unix sar command.I'm going to work on -c,-r and -u commands.I'm asked to find miminum,maximum and average of each column of sar output.I'm working on sar -c command now.I just want you to help me to find the average of "proc/s" column.

The links:
program: http://rapidshare.com/files/7243665/sar.sh
input file is created from a .sa raw data file(sar -c -f ornek2.sa > ornek2-c.txt): http://rapidshare.com/files/7243666/ornek2-c.txt

matthewg42 12-12-2006 06:06 PM

This rapidshare thing is a massive pain in the backside. needs javascript, presents captchas, bombards me with "buy premium service", ugly and confusing...

Please consider pastebin or nopaste. Or if it's not too long, just post in [code] tags in the thread.

hta1984 12-13-2006 02:30 AM

bash code: http://www.rafb.net/paste/results/8Zfn8P85.html
ignore the 3rd line of the code(that's how i generated the input file)
input file(ornek2-c.txt): http://www.rafb.net/paste/results/xKMpYT30.html

matthewg42 12-13-2006 11:00 AM

I'd say that this is really the sort of task which is much better accomplished using Perl:
Code:

#!/usr/bin/perl -w

use strict;

my $date  = "";
my $total = 0;
my $count = 0;

while(<>) {
    # remove the \n at the end of the line.
    chomp;

    if ( $. == 1 ) {
        # substitute everything up to the last space with nothing
        s/^.* //;
        $date = $_;
        next;
    }

    if ( /^\d+:\d+:\d+\s+(\d+\.\d+)$/ ) {
        $total += $1;
        $count++;
    }
}

printf( "date=$date; total=$total; count=$count; mean=%5.3f\n", ($total/$count) );

Feed the sar lofgile into it in stdin, or specify the filename on the command line.

hta1984 12-13-2006 11:59 AM

thank you for replying but i'm asked to do that job with bash

matthewg42 12-13-2006 01:02 PM

OK, some commends on your script:
  1. Line 9: using cut like this to extract the date simply isn't going to work properly. The file ornek2-c.txt contains many lines. You'll end up with all of them in the output. You already have this line in the myline variable, so you should use this as input, not the whole file. There is a shell variable expansion modifier which will chop off the leading characters of a variable, and you can use glob-style patterns in this. Since the date is preceded by the tab character, you can use this:
    Code:

    echo "The date is ${myline##*<TAB>}"
    Where <TAB> is a real tab character. However, I suspect there could be cases where there is no tab character depending on the length of the host name. To be safe it might be better to use awk with myline as the input:
    Code:

    dt=$(echo "$myline" | awk '{ print $NF }')
    echo "The date is $dt"

  2. Line 17: there's no need to invoke an external command (expr). The shell can do integer arithmatic quite OK:
    Code:

    let i+=1
    or
    Code:

    i=$(( $i + 1 ))
    according to your tastes.
  3. Lines 12-15. What's with all these temporary files. They are slow, and risky. Don't create temporary files unless you need to. Instea, I'd want to store the count so far in a variable. Declare the variable before the while loop starts, and 0 it. Then it becomes a two stage process: 1. extract the number from the lines you want, 2. add the number. Extracting could be done like with the date example above (be sure to exclude the line with the average!). The addition will need to be done with bc since bash doesn't support floating point math internally. By the way, zsh does support floating point math, which would make your script a lot nicer. You provided a good example of using bc in the original post.
  4. You should keep a track of the count of items you've added to your total. Calculating the mean is then very simple.

...BUT shell is really not ideal for this sort of number crunching. Having to invoke an external program for every line of input will lead to woefully bad performance for larger input files. The shell is good for a lot of stuff, but really not this sort of thing. Perl or awk are more ideal and installed on most any unix-like OS you will encounter.

I assume since bash was mandated this is a sort of homework (Hence me not posting a complete script). Get it working in bash, but tell them it is not ideal for this sort of tasks for the reasons I mentioned, and any more you can think of. You might even get extra marks. That or you'll be marked down for making the person who set the problem look silly. Ho hum.

hta1984 12-13-2006 02:34 PM

Quote:

Originally Posted by matthewg42
...BUT shell is really not ideal for this sort of number crunching. Having to invoke an external program for every line of input will lead to woefully bad performance for larger input files. The shell is good for a lot of stuff, but really not this sort of thing. Perl or awk are more ideal and installed on most any unix-like OS you will encounter.

I assume since bash was mandated this is a sort of homework (Hence me not posting a complete script). Get it working in bash, but tell them it is not ideal for this sort of tasks for the reasons I mentioned, and any more you can think of. You might even get extra marks. That or you'll be marked down for making the person who set the problem look silly. Ho hum.

if i was not restricted with language,i would prefer c,c++ or java.but our dear :) research assistants are not tolerant about their homeworks(i hope they won't read my post :D).And about my homework: in another post makyo helped me to fix my bash problem.Thank you very much about your interest,sharing your knowledge and spending your time with my question.


All times are GMT -5. The time now is 08:58 AM.