The tag has no usage guidance.

learn more… | top users | synonyms

1
vote
1answer
24 views

Wrong output in comparing floats

I'm trying to compare two floats in bash and something is going wrong. Here is the code sample based on solution here num1=0.502E-01 num2=0.01 echo $num1'>'$num2 | bc -l echo $num2'>'$num1 | bc ...
4
votes
2answers
277 views

Float operations with bc not precise?

When I'm performing float operations in shell using bc, the result is not the same if I use a regular calculator. Am I doing something wrong? For example, I need to find a volume of a sphere. User ...
0
votes
0answers
27 views

Multicore benchmark

I'm looking for a way to benchmark a multi-core CPU. I found this #!/bin/bash echo "scale=$1; a(1)*4" | bc -l The first argument is number of digits to calculate. Now we can run in parallel (4 ...
0
votes
0answers
27 views

Internal fragmentation of a file

One of the exercises which I have to do is to show the appearance of internal fragmentation in a file system by calculating in bytes the size of stored space by a non-empty regular file, but not used ...
3
votes
2answers
271 views

How can I do basic calculations in a shell script?

I want to perform some mathematical operations in the shelll. For example: 5+50*3/20 + (19*2)/7 I tried: #!/bin/bash read equ echo "scale=3; $equ" | bc -l Expected output: 17.929 My ...
57
votes
16answers
73k views

Is there a way to get the min, max, median, and average of a list of numbers in a single command?

I have a list of numbers in a file, one per line. How can I get the minimum, maximum, median and average values? I want to use the results in a bash script. Although my immediate situation is for ...
0
votes
0answers
28 views

`bc`: exponentiation of an empty expression

Typing ^0 into the bc prompt results in a syntax error, but only the first time this mistake is made: bc 1.06.95 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc. This ...
5
votes
3answers
184 views

How to format floating point number with exactly 2 significant digits in bash?

I want to print the floating point number with exactly two significant digits in bash (maybe using a common tool like awk, bc, dc, perl etc.). Examples: 76543 should be printed as 76000 0.0076543 ...
14
votes
8answers
28k views

Doing simple math on the command line using bash functions: $1 divided by $2 (using bc perhaps)

Sometimes I need to divide one number by another. It would be great if I could just define a bash function for this. So far, I am forced to use expressions like echo 'scale=25;65320/670' | bc but ...
0
votes
1answer
23 views

Piping difference of grep -c's with different regex to bc

I need to find the number of words in a file (1 word per line) that do not contain some set of letters. To do this I want to take the difference of grep -c '.' <file> and grep -c ...
1
vote
3answers
118 views

Shell Scripting: calculate power of a number with a real number as an exponent

I am using shell scripting and I am using the following expression: A=`echo "(( (($a / $b) ^ 0.3) -1 ))" |bc -l` I want to have a real number as an exponent. I noticed that If I place 0.3, it ...
0
votes
2answers
43 views

Is there an equivalent of javascript Math.min() in bash/bc etc.? [duplicate]

I need to convert some javascript calculations to bash, and I am wondering if there is an equivalent function of javascript Math.min() in bash, bc or any other calculator in the shell?
0
votes
0answers
28 views

Usage of the <<< operator [duplicate]

I recently ran across an example usage of the bc to output an expression like this: $ bc <<< "1+1" Which is the equivalent of: $ echo "1+1" | bc Does the <<< operator just ...
3
votes
3answers
62 views

A bc problem about long expression

I want to calculate an expression in shell. I use the following code: pi=$(echo "scale=10; 4*a(1)" | bc -l) i=3 d=`expr (1+c($pi*($i/10)+$pi))/2 | bc -l` But it says bad pattern: ...
1
vote
4answers
40 views

How to calculate % of successful queries

I am trying to calculate what is the % of successful queries in apache log. I have two commands: cat access_log|cut -d' ' -f10|grep "2.."|wc -l and cat access_log|cut -d' ' -f10|wc -l They ...
1
vote
4answers
7k views

How to round or convert a float value to int with bc? getting: “(standard_in) 1: syntax error”

I will get value like 2743410360.320 and I want value like 2743410360 to a variable. I tried INTValueOfGB=$ echo "($gb+0.5)/1" | bc But I am getting (standard_in) 1: syntax error
3
votes
3answers
16k views

How to calculate values in a shell script?

I run this command in the terminal: grep "bla bla blah" blah* | echo "Blah: $(wc -l) / $(ls | wc -l) * 100" And I get this output: Blah: 44 / 89 * 100 What I expect to see: 49.4 Is there a ...
0
votes
2answers
60 views

Any way to customize the initialization of variables in the bc calculator?

The bc calculator appears to assign uninitialized variables the value of zero. I would like to alter this behavior so that when bc encounters an uninitialized variable, it instead flags the expression ...
1
vote
3answers
128 views

bc scale: How to avoid rounding? (Calculate small binomial probability)

Following code calculates the Binomial Probability of a success event k out of n trials: n=144 prob=$(echo "0.0139" | bc) echo -e "Enter no.:" read passedno k=$passedno nCk2() { num=1 ...
0
votes
1answer
321 views

Why isn't this `expr … | bc -l` command working?

I'm noticing an error: bash: syntax error near unexpected token `-105.5*7+50*3' When executing the below script/expression: expr (-105.5*7+50*3)/20 + (19^2)/7 | bc -l Is there any other way ...
9
votes
3answers
2k views

Understand “ibase” and “obase” in case of conversions with bc?

I often use bc utility for converting hex to decimal and vice versa. However, it is always bit trial and error how ibase and obase should be configured. For example here I want to convert hex value C0 ...
3
votes
2answers
626 views

How to show zero before decimal point in bc?

echo "scale=3;1/8" | bc shows .125 on the screen. How to show 0.125 if the output result is less than one?
4
votes
4answers
612 views

Difficulty converting from binary to hexadecimal using bc

I read how to calculate using the command line calculator and a HERE-document, but nevertheless I do not get what I expected and can not find my error, what I did in the shell was: bc << HERE ...
0
votes
3answers
162 views

How to save in a variable the convert from decimal to binary without use an echo

I've been trying to save in a variable the value of the convert of a number from decimal to binary, like this: num1=10 echo "obase=2;$num1" | bc | tee -a register.txt but I don't want to show it ...
1
vote
1answer
148 views

bash: -eq vs. == and `bc` output type

This snippet of code has been taken from a larger script, (humbly) simulating the non-existing abs() function: [[ $(echo "$val < 0" | bc) -eq 1 ]] && val=$(echo "$val * -1" | bc) So we ...
2
votes
4answers
456 views

Anyway to pass a variable to bc, having a command to be executed?

I need to pass certain variable to bc to get the output in floating point, var1=$((<some operation>)) var2=$((<some operation>)) #Needs var1 var3=$((<some operation>)) ...
1
vote
1answer
316 views

How do you derive a decimal from a math operation involving a variable?

I'm trying to find derive SCALEFACTOR which is basically 10000/(sum of 4th column in a file). How do I get a decimal from the output? Appreciate any help in advance. #!/bin/bash ...
0
votes
1answer
367 views

Convert serialized Java object to human readable

I have object of class class X { private DateTime dt; "constructor, set/get" } I have one instance of this object serialized in file.bin. I want to show content of `file.bin on the Linux ...
7
votes
5answers
2k views

Store 2^500 in a variable in bash

I want to store the value of 2^500 in the variable DELTA. I'm doing export DELTA=$(echo "scale=2; 2^500" | bc) but this does not set DELTA to ...
3
votes
8answers
370 views

bc output binary as nibbles separated by whitespace

I'm debugging code which contains quite a few bit shift operations, and I'm using bc a lot to look at what's happening on the bit level. Here's what I use: $ echo 'obase=2;598980975283696640' | bc ...
5
votes
2answers
4k views

Bash limiting precision of floating point variables

In Ubuntu 14.04.1 LTS 64-bit bash I am declearing floating point variables by multiplying floating point bash variables in bc with scale set to 3; however, I cannot get the number of digits after the ...
2
votes
1answer
199 views

Problematic bc calculation in shell script

On Ubuntu 14.04.1 64-bit LTS I am writing a shell script and if I define the start of the sequence used in the for loop with a variable instead of a constant I get really weird behavior and there are ...
1
vote
3answers
853 views

Evaluation an expression and rounding up to three decimals

I have an expression "5+50*3/20 + (19*2)/7" I need to round it up to 3 decimal places. The answer to this is 17.92857142857143. When I use the script below it is giving me 17.928. The answer should be ...
3
votes
1answer
403 views

Split input file into X parts of size total lines / X

Currently, my bash script splits by number of lines. However, I'd like to split a file into X pieces, each of those having total lines equal to the file length divided by X. The script is run as ...
28
votes
4answers
2k views

python vs bc in evaluating 6^6^6

I am evaluating the expression 6^6^6 using python and bc separately. The content of the python file is print 6**6**6. When I execute time python test.py, I get the output as real 0m0.067s ...
0
votes
0answers
1k views

BC returns syntax error

#!/bin/bash cd ~/simplesim-3.0/benchmarks INSTS=$(grep "sim_num_insn" ./anagram.txt | awk '{print $2}'); IL1_ACCESSES=$(grep "il1.accesses" ./anagram.txt | awk '{print $2}'); IL1_MISSRATE=$(grep ...
2
votes
1answer
243 views

bc behaves differently on Solaris and Linux

I have the following problem that I have not resolved for a long time now. We have a Linux (x86_64 GNU/Linux) server and a Solaris (SunOS 5.10 i86pc Solaris) server where I work. On the linux server, ...
0
votes
2answers
97 views

Sum up the selected fields

I have a file with a numeric value in character positions 86 to 99. I want to sum up the entire amount present in the file, only for lines which contain RS at position 124-125. I am trying to do like ...
9
votes
1answer
10k views

Does bc support hex calculations?

I'm trying to do a hex calculation directly with bc, I already specified the scale. echo 'scale=16;c06b1000-c06a5e78' | bc But I still get a zero. What could be wrong?
7
votes
2answers
3k views

Cannot sum numbers received from stdin using bc

I'm trying to calculate the average entropy of files contained in a folder using: { echo '('; find . -type f -exec entropy {} \; | grep -Eo '[0-9.]+$' | sed -r 's/$/+/g'; echo '0)/'; find . -type f | ...
9
votes
3answers
6k views

How is bc different from dc?

What are the differences between dc and bc calculators? When should I use dc and when bc?
5
votes
5answers
344 views

Automatic transformation of newlines in shell variable assignment

This came out of one of my comments to this question regarding the use of bc in shell scripting. bc puts line breaks in large numbers, e.g.: > num=$(echo 6^6^3 | bc) > echo $num ...
6
votes
2answers
544 views

how to add command line calculator to bash that uses comma as decimal mark?

How can I add a command line calculator to my bash? I have found some, but all of them use the full stop as decimal mark, but I want to have it to use the comma as decimal mark as most of the world ...
0
votes
2answers
2k views

bc removes decimal point

This is related to Shell script too slow for output to Conky This code is nearly perfect: stdbuf -oL jack_cpu_load \ | grep --line-buffered "jack DSP load" \ | stdbuf -oL cut -d' ' -f4 \ ...
4
votes
4answers
14k views

Perform floating point arithmetic in shell script variable definitions [duplicate]

I understand bash and some other interpreters only perform arithmetic for integers. In the following for loop, how can I accomplish this? I've read that bc can be used but am not sure how to use bc ...
2
votes
2answers
3k views

How to sum a bash array of numbers (some in scientific notation)?

Since the following command using bc does not work for numbers in scientific notation, I was wondering about an alternative, e.g. using awk? sum=$( IFS="+"; bc <<< "${arrValues[*]}" )
2
votes
3answers
1k views

BC - no “\\n” at the end + start with zeros?

This is a bc output, e.g.: Input: echo "scale=10; BLA-BLA-HERE-NOT-IMPORTANT" | bc Output: .3708446283953709207058828124021300754352578903651372655882743141882\ ...
4
votes
1answer
357 views

BC not handling scale = 0 correctly

I defined the cbrt function to return a cube root. I need to get an integer back (even if it is close to the cube root, it will be acceptable in my case). However, when I put the scale as 0 to get an ...
3
votes
4answers
1k views

Round/truncate digit in string in zsh (or with external tool)

I'm trying to do an interface to bc so it can be used intuitively and without the annoyance of getting "stuck" in it. I haven't got around to test it that much, because I got stuck on another detail, ...
2
votes
2answers
3k views

BC—automatic full precision multiplication

High, I need to test my arbitrary precision calculator, and bc seems like a nice yardstick to compare to, however, bc does truncate the result of each multiplication to what seems to be the maximum ...