bash shell——sum

#!/bin/bash
# sum.sh
# 获取随机数量的参数,相加并打印结果
#
total=0
#
# $# 表示参数的数量
# for 循环获取每个参数
# ${!i} 表示返回第i个参数
#
for (( i = 1; i <= $#; i++ ))
do
    total=$[ $total + ${!i} ]
done
#
echo Total = $total
#

 

#!/bin/bash
# 使用$@相加所有参数
# sum1.sh
#
total=0
#
for param in $@
do
    total=$[ $param + $total ]
done
echo
echo Total = $total
echo

 

posted @ 2019-09-15 13:29  no樂on  阅读(907)  评论(0)    收藏  举报