如何在 Bash shell 脚本中保存更多的变量?
How do I save greater number in variable in Bash shell script?
我有一个 bash shell 应用程序可以计算 rx 和 tx 数据包。这些数据包每秒都有变化,然后将其保存在 SUM 变量中。我的目标是在新变量中保存更多的数字。我该怎么做?
SUM=$(expr $TKBPS + $RKBPS)
now=$(( $SUM))
if [ $now -gt $SUM ] ; then
max=$(( $now ))
fi
echo "SUM is: $SUM"
echo "MAX is: $max"
#!/bin/bash
# Example of a previously calculated SUM that gives MAX number 5
max=5
SUM=$(($A + $B))
# Update max only if new sum is greater than it
[ $SUM -gt $max ] && max=$SUM
echo $max
示例:
$ A=5 B=9 bash program
14
$ A=1 B=1 bash program
5
您的代码中的错误是:if [ $now -gt $max ]
(最大值,而不是 SUM)。
你可以这样写更好:
sum=$((TKBPS+RKBPS))
max=$((sum>max?sum:max))
# ((sum>max)) && max=$sum # or like this
echo "sum is $sum"
echo "max is $max"
我有一个 bash shell 应用程序可以计算 rx 和 tx 数据包。这些数据包每秒都有变化,然后将其保存在 SUM 变量中。我的目标是在新变量中保存更多的数字。我该怎么做?
SUM=$(expr $TKBPS + $RKBPS)
now=$(( $SUM))
if [ $now -gt $SUM ] ; then
max=$(( $now ))
fi
echo "SUM is: $SUM"
echo "MAX is: $max"
#!/bin/bash
# Example of a previously calculated SUM that gives MAX number 5
max=5
SUM=$(($A + $B))
# Update max only if new sum is greater than it
[ $SUM -gt $max ] && max=$SUM
echo $max
示例:
$ A=5 B=9 bash program
14
$ A=1 B=1 bash program
5
您的代码中的错误是:if [ $now -gt $max ]
(最大值,而不是 SUM)。
你可以这样写更好:
sum=$((TKBPS+RKBPS))
max=$((sum>max?sum:max))
# ((sum>max)) && max=$sum # or like this
echo "sum is $sum"
echo "max is $max"