Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在bash中添加数字_Bash - Fatal编程技术网

在bash中添加数字

在bash中添加数字,bash,Bash,我正在收集两台IRC服务器的连接帐户: s1a=`sockstat|grep 94.125.182.255|grep 93. -c` echo "Budapest.Hu.Eu.Undernet.org" $s1a "connections" s2a=`sockstat|grep 154.35.175.201|grep 93. -c` echo "Chicago.IL.US.Undernet.Org" "connections" 我如何计算总数(这样,$total等于$s1a+$s2a)?如果

我正在收集两台IRC服务器的连接帐户:

s1a=`sockstat|grep 94.125.182.255|grep 93. -c`
echo "Budapest.Hu.Eu.Undernet.org" $s1a "connections"
s2a=`sockstat|grep 154.35.175.201|grep 93. -c`
echo "Chicago.IL.US.Undernet.Org"  "connections"

我如何计算总数(这样,
$total
等于
$s1a+$s2a
)?

如果我理解您的问题,您所需要的就是shell算法。试一试

total=$(( $s1a + $s2a ))
注意,
=
符号和定义变量(
total
)周围没有空格,也没有前导的
$

正如超级程序员提醒我们的那样,您甚至不需要在
$(…)
中的var名称上使用
$
,因此

total=$(( s1a + s2a ))
echo "$total conexion"
为了使主要代码块现代化,我建议不要使用cmd替换的back-tic形式,而是切换到

s1a=$(sockstat|grep 94.125.182.255|grep 93. -c)
如果您需要嵌套cmd替换,您会很高兴切换;-)


IHTH

你的问题/问题是什么?代码为$s1a+$s2a=$total conexionI我试图让英语使用者更清楚地了解你的问题。请让我知道我是否准确评估了你的意图。