Numbers and Arithmetic in bash: Difference between revisions
Jump to navigation
Jump to search
(Created page with "=Internal= * bash * Integers in bash") |
No edit summary |
||
Line 3: | Line 3: | ||
* [[bash#Subjects|bash]] | * [[bash#Subjects|bash]] | ||
* | =How to Tell if a Variable has an Integer Value= | ||
<pre> | |||
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 | |||
</pre> | |||
=Numerical Calculations= | |||
Multiplication: | |||
<pre> | |||
local operand1=10.1 | |||
local operand2=20.2 | |||
result=$(echo ${operand1} ${operand2} | awk '{printf "%5.1f\n",$1*$2}') || exit 1 | |||
</pre> | |||
Division: | |||
<pre> | |||
local operand1=10.1 | |||
local operand2=20.2 | |||
result=$(echo ${operand1} ${operand2} | awk '{printf "%5.1f\n",$1/$2}') || exit 1 | |||
</pre> |
Revision as of 19:47, 8 June 2016
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
Numerical Calculations
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