Numbers and Arithmetic in bash: Difference between revisions
Jump to navigation
Jump to search
Line 60: | Line 60: | ||
{{Internal|Bash script that generates a random MAC address|Bash script that generates a random MAC address}} | {{Internal|Bash script that generates a random MAC address|Bash script that generates a random MAC address}} | ||
=Organizatorium= | |||
<syntaxhighlight lang='bash'> | |||
local t0="$(date -u +%s)" | |||
local t1="$(date -u +%s)" | |||
local delta="$(bc <<<"${t1}-${t0}")" | |||
</syntaxhighlight> |
Revision as of 04:54, 1 May 2020
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 about
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
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 [[ $(expr ${a} - ${b}) < 0 ]]; then
...
fi
Random Numbers
Organizatorium
local t0="$(date -u +%s)"
local t1="$(date -u +%s)"
local delta="$(bc <<<"${t1}-${t0}")"