Bootstrap

shell重复执行指定次数

整理一个通过while实现循环的shell脚本,通过运行后输入指定运行次数完成循环。

#!/bin/bash
num=1
read -p "请输入需要执行的ping次数:" testnumber
length=`echo ${testnumber} | wc -L`
testnumber=`expr $testnumber + 1`
#if [ "$testnumber" -eq 7 ];then

#fi
echo "num="$num
while :
do
      if [ $num -ge $testnumber ]
          then
                  echo "exit"
                  exit
          else
              let num++  #let对整数进行计算
                  echo "num="$num" num++"
          fi
done

在这里插入图片描述
shell打印百分比

awk 'BEGIN{printf "Scheitern(%)= %.2f%%\n",('$num_err'/'$testnumber')*100}'

shell建立子函数

#!/bin/bash
# author:菜鸟教程
# url:www.runoob.com

funWithReturn(){
    echo "这个函数会对输入的两个数字进行相加运算..."
    echo "输入第一个数字: "
    read aNum
    echo "输入第二个数字: "
    read anotherNum
    echo "两个数字分别为 $aNum 和 $anotherNum !"
    return $(($aNum+$anotherNum))
}
funWithReturn
echo "输入的两个数字之和为 $? !"
;