Numbers and Arithmetic in bash: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(9 intermediate revisions by the same user not shown)
Line 5: Line 5:
=How to Tell if a Variable has an Integer Value=
=How to Tell if a Variable has an Integer Value=


<pre>
==With <tt>printf</tt>==
 
<code>printf</code> invoked with <code>%d</code> will attempt to print the argument as an integer. If the argument is not an integer, <code>printf</code> will return a non-zero exit value.
 
<syntaxhighlight lang='bash'>
arg="22"
if printf %d "${arg}" >/dev/null 2>&1; then
    echo "${arg} is an int"
else
    echo "${arg} is NOT an int"
fi
</syntaxhighlight>
 
==With <tt>expr</tt>==
 
<syntaxhighlight lang='bash'>
v=10
v=10


Line 17: Line 32:
     #
     #
fi
fi
</pre>
</syntaxhighlight>


More details: {{Internal|expr|expr}}
More details: {{Internal|expr|expr}}
Line 24: Line 39:


==$((...))==
==$((...))==
<code>$((...))</code> performs the arithmetic operation using shell variable names or strings and returns the result at stdout.


<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
Line 30: Line 47:
echo $((op1 + op2))
echo $((op1 + op2))
echo $((op1 - op2))
echo $((op1 - op2))
echo $((op1 * op2))
echo $((op1 * op2))  
echo $((op1 / op2))
echo $((op1 / op2)) # integral division
echo $((op1 % op2)) # modulo
echo $((op1 += op2))
</syntaxhighlight>
</syntaxhighlight>
displays:
displays:
Line 40: Line 59:
2
2
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang='bash'>
result=$(("$(date '+%s')" % 2))
</syntaxhighlight> also works.


==expr==
==expr==
Line 55: Line 77:
Multiplication:
Multiplication:


<pre>
<syntaxhighlight lang='bash'>
local operand1=10.1
local operand1=10.1
local operand2=20.2
local operand2=20.2


result=$(echo ${operand1} ${operand2} | awk '{printf "%5.1f\n",$1*$2}') || exit 1
result=$(echo ${operand1} ${operand2} | awk '{printf "%5.1f\n",$1*$2}') || exit 1
</pre>
result=$(echo ${operand1} ${operand2} | awk '{printf "%.1f\n",$1*$2}') || exit 1
</syntaxhighlight>


Division:
Division:


<pre>
<syntaxhighlight lang='bash'>
local operand1=10.1
local operand1=10.1
local operand2=20.2
local operand2=20.2


result=$(echo ${operand1} ${operand2} | awk '{printf "%5.1f\n",$1/$2}') || exit 1
result=$(echo ${operand1} ${operand2} | awk '{printf "%5.1f\n",$1/$2}') || exit 1
</pre>
</syntaxhighlight>


=Increment an Integer=
=Increment an Integer=
Line 82: Line 105:
a=1
a=1
b=2
b=2
if [[ $((a - b)) -lt 0 ]]; then
  ...
fi
if [[ $(expr ${a} - ${b}) < 0 ]]; then
if [[ $(expr ${a} - ${b}) < 0 ]]; then
   ...
   ...

Latest revision as of 20:05, 7 August 2024

Internal

How to Tell if a Variable has an Integer Value

With printf

printf invoked with %d will attempt to print the argument as an integer. If the argument is not an integer, printf will return a non-zero exit value.

arg="22"
if printf %d "${arg}" >/dev/null 2>&1; then
    echo "${arg} is an int"
else
    echo "${arg} is NOT an int"
fi

With expr

v=10

if expr ${v} + 1 >/dev/null 2>&1; then
    #
    # the value contained by v is an integer
    #
else
    #
    # the value contained by v is NOT an integer
    #
fi

More details:

expr

Integral Number Operations

$((...))

$((...)) performs the arithmetic operation using shell variable names or strings and returns the result at stdout.

op1=4
op2=2
echo $((op1 + op2))
echo $((op1 - op2))
echo $((op1 * op2)) 
echo $((op1 / op2)) # integral division
echo $((op1 % op2)) # modulo
echo $((op1 += op2))

displays:

6
2
8
2
result=$(("$(date '+%s')" % 2))

also works.

expr

expr

bc

local t0="$(date -u +%s)"
local t1="$(date -u +%s)"
local delta="$(bc <<<"${t1}-${t0}")"

Floating Point Operations

Multiplication:

local operand1=10.1
local operand2=20.2

result=$(echo ${operand1} ${operand2} | awk '{printf "%5.1f\n",$1*$2}') || exit 1
result=$(echo ${operand1} ${operand2} | awk '{printf "%.1f\n",$1*$2}') || exit 1

Division:

local operand1=10.1
local operand2=20.2

result=$(echo ${operand1} ${operand2} | awk '{printf "%5.1f\n",$1/$2}') || exit 1

Increment an Integer

Increment an Integer with ((...)) Operator

Decrement an Integer

Decrement an Integer with ((...)) Operator

Numeric Evaluation in a Condition

a=1
b=2

if [[ $((a - b)) -lt 0 ]]; then
  ...
fi

if [[ $(expr ${a} - ${b}) < 0 ]]; then
  ...
fi

Random Numbers

Bash script that generates a random MAC address