Bash:将负数的所有实例显示为零
Bash: display all instances of negative numbers as zero
经过一些数学运算后,我想将所有出现的负数显示为零。简单例子:
num1=6
num2=8
firstresult=$(( $num1 - $num2 ))
echo $result
#
num3=2
num4=9
secondresult=$(( $num3 - $num4 ))
echo $secondresult
等等...显然,两个结果变量都会产生负数。我希望这些数字显示为零。
也许要写一些函数?不确定。感谢任何帮助。
no_negatives () {
echo "$(( < 0 ? 0 : ))"
}
这个名字太土了,随便选一个好点的吧。那么:
$ a=5 b=3
$ no_negatives $((a - b))
2
$ no_negatives $((b - a))
0
经过一些数学运算后,我想将所有出现的负数显示为零。简单例子:
num1=6
num2=8
firstresult=$(( $num1 - $num2 ))
echo $result
#
num3=2
num4=9
secondresult=$(( $num3 - $num4 ))
echo $secondresult
等等...显然,两个结果变量都会产生负数。我希望这些数字显示为零。
也许要写一些函数?不确定。感谢任何帮助。
no_negatives () {
echo "$(( < 0 ? 0 : ))"
}
这个名字太土了,随便选一个好点的吧。那么:
$ a=5 b=3
$ no_negatives $((a - b))
2
$ no_negatives $((b - a))
0