LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-12-2006, 01:59 PM   #1
hta1984
LQ Newbie
 
Registered: Apr 2004
Location: Ankara
Distribution: suse
Posts: 9

Rep: Reputation: 0
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
 
Old 12-12-2006, 02:58 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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
 
Old 12-12-2006, 05:25 PM   #3
hta1984
LQ Newbie
 
Registered: Apr 2004
Location: Ankara
Distribution: suse
Posts: 9

Original Poster
Rep: Reputation: 0
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
 
Old 12-12-2006, 06:06 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 12-13-2006, 02:30 AM   #5
hta1984
LQ Newbie
 
Registered: Apr 2004
Location: Ankara
Distribution: suse
Posts: 9

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

Last edited by hta1984; 12-13-2006 at 03:52 AM.
 
Old 12-13-2006, 11:00 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 12-13-2006, 11:59 AM   #7
hta1984
LQ Newbie
 
Registered: Apr 2004
Location: Ankara
Distribution: suse
Posts: 9

Original Poster
Rep: Reputation: 0
thank you for replying but i'm asked to do that job with bash
 
Old 12-13-2006, 01:02 PM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 12-13-2006, 02:34 PM   #9
hta1984
LQ Newbie
 
Registered: Apr 2004
Location: Ankara
Distribution: suse
Posts: 9

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


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
Parse String in a Bash script jimwelc Linux - Newbie 8 11-09-2012 07:47 AM
What's the meaning of "PHP Parse error: parse error, unexpected $ in..." frandalla Programming 23 03-04-2009 12:34 PM
(standard_in) 1: parse error timmay9162 Programming 8 04-26-2006 02:32 AM
bash script help to parse out text slack guy Linux - Newbie 3 12-30-2004 08:42 AM
Newbie bash path parse problem ericcarlson Linux - Newbie 4 08-05-2004 10:43 AM

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

All times are GMT -5. The time now is 03:00 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