Numbers and Arithmetic in bash: Difference between revisions
Jump to navigation
Jump to search
(23 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= | ||
< | <syntaxhighlight lang='bash'> | ||
v=10 | v=10 | ||
Line 17: | Line 17: | ||
# | # | ||
fi | fi | ||
</ | </syntaxhighlight> | ||
More | More details: {{Internal|expr|expr}} | ||
=Integral Number Operations= | |||
==$((...))== | |||
<syntaxhighlight lang='bash'> | |||
op1=4 | |||
op2=2 | |||
echo $((op1 + op2)) | |||
echo $((op1 - op2)) | |||
echo $((op1 * op2)) | |||
echo $((op1 / op2)) # integral division | |||
echo $((op1 % op2)) # modulo | |||
</syntaxhighlight> | |||
displays: | |||
<syntaxhighlight lang='bash'> | |||
6 | |||
2 | |||
8 | |||
2 | |||
</syntaxhighlight> | |||
<syntaxhighlight lang='bash'> | |||
echo $(($(date '+%s') % 2)) | |||
</syntaxhighlight> also works. | |||
==expr== | |||
{{Internal|expr|expr}} | |||
==bc== | |||
<syntaxhighlight lang='bash'> | |||
local t0="$(date -u +%s)" | |||
local t1="$(date -u +%s)" | |||
local delta="$(bc <<<"${t1}-${t0}")" | |||
</syntaxhighlight> | |||
=Floating Point Operations= | =Floating Point Operations= | ||
Line 25: | Line 59: | ||
Multiplication: | Multiplication: | ||
< | <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 | ||
</ | result=$(echo ${operand1} ${operand2} | awk '{printf "%.1f\n",$1*$2}') || exit 1 | ||
</syntaxhighlight> | |||
Division: | Division: | ||
< | <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 | ||
</ | </syntaxhighlight> | ||
=Increment an Integer= | =Increment an Integer= | ||
{{Internal|((...))#Increment_an_Integer|Increment an Integer with ((...)) Operator}} | |||
=Decrement an Integer= | |||
{{Internal|((...))#Decrement_an_Integer|Decrement an Integer with ((...)) Operator}} | |||
=Numeric Evaluation in a Condition= | |||
<syntaxhighlight lang='bash'> | |||
a=1 | |||
b=2 | |||
if [[ $((a - b)) -lt 0 ]]; then | |||
... | |||
fi | |||
if [[ $(expr ${a} - ${b}) < 0 ]]; then | |||
... | |||
fi | |||
</syntaxhighlight> | |||
=Random Numbers= | |||
{{Internal|Bash script that generates a random MAC address|Bash script that generates a random MAC address}} | |||
Revision as of 04:56, 1 July 2021
Internal
How to Tell if a Variable has an Integer Value
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:
Integral Number Operations
$((...))
op1=4
op2=2
echo $((op1 + op2))
echo $((op1 - op2))
echo $((op1 * op2))
echo $((op1 / op2)) # integral division
echo $((op1 % op2)) # modulo
displays:
6
2
8
2
echo $(($(date '+%s') % 2))
also works.
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
Decrement an Integer
Numeric Evaluation in a Condition
a=1
b=2
if [[ $((a - b)) -lt 0 ]]; then
...
fi
if [[ $(expr ${a} - ${b}) < 0 ]]; then
...
fi