The arithmetic tag has no wiki summary.
11
votes
6answers
15k 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 ...
10
votes
5answers
4k views
How to compare to floating point number in a shell script
I want to compare two floating point numbers in a shell script. The following code is not working:
#!/bin/bash
min=12.45
val=10.35
if (( $val < $min )) ; then
min=$val
fi
echo $min
9
votes
3answers
409 views
Why does “$(( ~33 ))” produce -34?
$ echo $(( 255 ))
255
$ echo $(( 33 ))
33
$ echo $(( ~33 ))
-34
$ echo $(( ~255 ))
-256
$
and my kernel is:
$ uname -a
Linux HOSTNAME 3.2.0-40-generic-pae #64-Ubuntu SMP Mon Mar 25 21:44:41 UTC ...
8
votes
2answers
1k views
Bash: double equals vs -eq
I am doing integer comparison in bash (trying to see if the user is running as root), and I found two different ways of doing it:
Double equals:
if [ $UID == 0 ]
then
fi
-eq
if [ $UID -eq 0 ]
...
7
votes
4answers
1k views
creating a sequence of numbers, one per line in a file
Is there a way to create out of thin air, a file that is a sequence of numbers, starting at a given number, one per line?
something like
magic_command start 100 lines 5 > b.txt
and then, b.txt ...
7
votes
7answers
751 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 ...
6
votes
2answers
346 views
Using bash “double paren” arithmetic expansion, math fails with leading zero
I have a simple script that deals with hours and minutes.
If I want to calculate number of minutes since midnight having a string s hh:mm I tried splitting string then doing hh * 60 + mm
My problem ...
6
votes
5answers
805 views
Binary to hexadecimal and decimal in a shell script
I have a context where I need to convert binary to hexadecimal and decimal and viceversa in a shell script. Can someone suggest me a tool for this?
6
votes
1answer
343 views
Why does a=0; let a++ return exit code 1?
Try it:
$ a=0
$ let a++
$ echo $?
1 # Did the world just go mad?
$ echo $a
1 # Yes, it did.
$ let a++
$ echo $?
0 # We have normality.
$ echo $a
2
Contrast with this:
$ b=0
$ let b+=1
$ echo $?
0
...
4
votes
2answers
1k views
Bash: integer expression expected, using read/test
I'm learning the basics of shell programming, so I wrote something simple like
#!/bin/bash
read var1
read var2
if [ var1 -lt var2 ]; then
echo "var1 is lt var2"
else
echo "var2 is lt var1"
fi
...
4
votes
3answers
104 views
how to suppress bash octal number interpretation? (to be interpreted as decimal)
I have this:
date +"%H hours and %M minutes"
I use festival to say it up.. but it says like: "zero nine hours".. I want it to say "nine hours"!
but date always give me 09... so I wonder if bash ...
4
votes
1answer
174 views
Emacs simple arithmetics in query replace
Is it possible to do the following in emacs:
Query replace all numbers in a region by the number +n, for example if you have:
12 and 7 are nice numbers
let n be 3, then it should transform to
15 ...
4
votes
2answers
148 views
Redirect stderr of the double-parentheses construct
I'd like to be able to redirect the stderr of a double-parentheses construct.
For example:
a=$(($var/$var2))
would output some error messages if $var2 = 0, I do not want the user to see this.
I ...
4
votes
3answers
121 views
bash -e exits when let or expr evaluates to 0
I have a bash script that sets -e so the script will exit on any exit status != 0.
I'm trying to do some basic shell arithmetic assigned to variables and sometimes the expression equals 0 which ...
4
votes
2answers
140 views
Trying to do arithmetic in Perl rename tool (Debian)
Supposed I have five mp3 files:
01-trackfoo.mp3
02-trackbar.mp3
03-trackbaz.mp3
04-trackabc.mp3
05-trackxyz.mp3
Now I give a quick listen to the files (just mplayer on console will do the trick) ...
3
votes
2answers
330 views
Why does $((40-35)) turn into 5?
What kind of strange shell syntax is hidden behind $((40-35)) and how come it calculates a value?
Example:
$> echo $((40-35))
5
3
votes
2answers
220 views
Error when subtracting two negative numbers in ksh: “assignment requires lvalue”
I am trying to debug someone else's script:
The code line is:
y=$((${oldvalue[$x]}-${newvalue[$x]}))
y gets calculated fine as long as both sides are positive numbers. However, I have a ...
3
votes
1answer
244 views
awk high precision arithmetic
I am looking for a way to tell awk to do high-precision arithmetic in a substitution operation. This involves, reading a field from a file and substituting it with a 1% increment on that value. ...
3
votes
3answers
3k 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 ...
3
votes
2answers
2k views
In bash, how to convert 8 bytes to an unsigned int (64bit LE)?
How can I 'read/interpret' 8 bytes as an unsigned int (Little Endian)?
Perhaps there is a Bash-fu magic conversion for this?
UPDATE:
It seems that something got cross-wired in the interpretation of ...
2
votes
4answers
685 views
Calculate variable, and output it to another variable
The only calculator I know is bc. I want to add 1 to a variable, and output to another variable.
I got the nextnum variable from counting string in a file:
nextnum=`grep -o stringtocount ...
2
votes
4answers
213 views
Generate lists of page numbers for 2-up duplex printing: 2,3,… and 1,4,…
How can I generate the following increments?
2,3,6,7,10,11,14,15,18,19,22,23,26,27,30,31,34,35,38,39,42,43,46,47,...,135
and
...
2
votes
2answers
330 views
How to divide a list of values by a number in command line?
I am trying to translate a simple program to the command line using unix utilities. For example, if I have a frequency list (after piping through uniq and sort)
5 x
4 y
1 z
I want to print out, ...
2
votes
3answers
1k views
grepping a variable and adding 1 to it
I have a variable containing a leading zeroes number, and I want to grep this variable and that same variable plus one. I made several attempts but ran into errors. Here's what I want to do:
read var
...
2
votes
1answer
74 views
Difference between two types of shell arithmetic
What is the difference between the following two shell arithmetic commands:
echo $[ $var1 - 1 ]
echo $(( $var1 - 1 ))
Assuming var1 = 5 for example.
1
vote
4answers
1k views
Increment number in bash variable string
VERSION="4.0beta12"
NEXT_VERSION=?????
Writing an automatic release script, I have the current version in a variable $VERSION, and I would like to calculate the next version as $NEXT_VERSION.
Rule: ...
1
vote
3answers
331 views
How to create a sequence with leading zeroes using brace expansion
When I use the following, I get a result as expected:
$ echo {8..10}
8 9 10
How can I use this brace expansion in an easy way, to get the following output?
$ echo {8..10}
08 09 10
I now that ...
1
vote
3answers
353 views
Writing a shell script to take output of an executable and perform some calculation
I am not much familiar with shell script. I want to write a shell script for the following pseudo-code:
min=some garbage value
for(i=1 to N){ // N and n will be taken as input for the shell script.
...
1
vote
2answers
78 views
case + how to implement equal or less or greater in case syntax
My target is to verify a range of number with (only with case + esac), and print the range. So for example:
If the number is between 0 and 80, print >=0<=80
If the number is between 81 and 100 ...
0
votes
2answers
177 views
How to add arithmetic variables in a script
I want to accumulate the line size of a number of files contained in a folder. I have written the following script:
let a=0
let num=0
for i in folder/*
do
num=`cat $i | wc -l`
...
-1
votes
1answer
1k views
Sum of grep results
I need to output the sum of a grep results for a certain time, for now i hard coded the time grep but any help will be very much appreciated, my problem is i cant output the results on the script.
If ...
