1819684 Members
3499 Online
109605 Solutions
New Discussion юеВ

bc

 
SOLVED
Go to solution
Nobody's Hero
Valued Contributor

bc

Good Morning,
I have written a script that reads a log file, calls 'ex' to edit what I need. Then I gather some backup stats and I need to do some division. How can I use bc in a script to devide, lets say, 400GB took 12 hours. Can I just call 'bc' and do the math?
bc
400 / 12
Or is there another way to calculate numbers?

Thanks,
Bob
UNIX IS GOOD
8 REPLIES 8
harry d brown jr
Honored Contributor
Solution

Re: bc

Robert,

Check this out:

bdf | grep -v Filesystem|grep vg02 | tr -s " " " "|cut -d" " -f 3 | sort -n | xargs echo | sed "s/ / + /g" | bc

or

echo `ls -l /tmp/va4_ioscan`"\nx x x x .65" | tr -s " " " "|cut -d" " -f 5 | xargs echo | sed "s/ / \* /g"|bc|cut -d"." -f1|sed "s/$/ \/ 512 /"|bc|sed "s:^:dd if=/t
mp/va4_ioscan of=/tmp/stewcount=:" | sh



live free or die
harry





Live Free or Die
Andreas Voss
Honored Contributor

Re: bc

Hi,

bc can used ie:

value=400
result=`echo "scale=2\n$value/12"|bc`

Regards
Peter Kloetgen
Esteemed Contributor

Re: bc

Hi Robert,

there are some more ways to calculate:

1) the "expr" command, has the following syntax: expr nr1 operator nr2
possible operators are: addition +
subtraction -
multiplication \*
division /
for example: expr 2 \* 5
# 10

please remember, this command can only calculate integers, so expr 10 / 6 would deliver an output of 1 !

2) (( nr1 operator nr2 )), has the same functions than expr, but is done a lot of faster. Same limitations than expr, no floating point!

3) awk has also a lot of numeric builtin- functions. To use it with variable=value, you call it awk -v variable=value, then the variable is a predefined in the awk- program. The awk- command has some numeric functions which can handle floating point.

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Robin Wakefield
Honored Contributor

Re: bc

Hi Bob,

If you have perl:

perl -e 'print (eval ($ARGV[0]),"\n")' 400/12
33.3333333333333

or whatever.

Rgds, Robin.
harry d brown jr
Honored Contributor

Re: bc

Robert,

If you use the "-l" (dash-el) option, you get a decimals:

[root]pbctst: echo "400 / 12" | bc -l
33.33333333333333333333
[root]pbctst:


live free or die
harry
Live Free or Die
James R. Ferguson
Acclaimed Contributor

Re: bc

Hi Robert:

'bc' works quite well. For decimal numbers, include the "scale":

# X=1;Y=8;echo "scale=3\n $X/$Y"|bc

I, too, like 'awk' given its ability to format and produce output in different number bases:

# X=1;Y=8;echo "$X $Y"|awk '{printf "%.4f\n",$1/$2}'

# X=7;Y=8;echo "$X $Y"|awk '{printf "%4x\n",$1+$2}'

For simple integer arithmetic, you can use the shell 'let':

# let A=100;let B=8;let C=$A/$B;echo $C

Regards!

...JRF...
Steve Steel
Honored Contributor

Re: bc

Hi


BC is fine.

Example
#!/usr/bin/sh
#You could use the 'two way pipe' to the program to do all your math operations,
like this:
# This starts bc with a two-way pipe
bc |&
# print -p writes to the pipe
print -p scale=4
print -p 3/4
# read -p reads from the pipe
read -p myvar
echo $myvar

bc tutorial in Number Processing Users Guide


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Nobody's Hero
Valued Contributor

Re: bc

Thanks Everyone,
Lots of great ideas that I will use.

Thanks,
Again,
Bob
UNIX IS GOOD