这个脚本为条件语句,用到的语句包括while ,read,if,break,不懂可以看看相关资料
while 结构允许重复执行一系列命令,只要控制 while 循环的命令执行成功(退出状态是零)。语法是:
while CONTROL-COMMAND; do CONSEQUENT-COMMANDS; done
#!/bin/bash
# Calculate the average of a series of numbers.
SCORE="0"
AVERAGE="0"
SUM="0"
NUM="0"
while true; do
  echo -n "Enter your score [0-100%] ('q' for quit): "; read SCORE;
  if (("$SCORE" < "0"))  || (("$SCORE" > "100")); then
    echo "Be serious.  Common, try again: "
  elif [ "$SCORE" == "q" ]; then
    echo "Average rating: $AVERAGE%."
    break
  else
    SUM=$[$SUM + $SCORE]
    NUM=$[$NUM + 1]
    AVERAGE=$[$SUM / $NUM]
  fi
done
echo "Exiting."
执行结果如下:
# ./pingjun.sh
Enter your score [0-100%] ('q' for quit): 10
Enter your score [0-100%] ('q' for quit): 30
Enter your score [0-100%] ('q' for quit): 20
Enter your score [0-100%] ('q' for quit): q
Average rating: 20%.
Exiting.
--------------------------------------
# ./pingjun.sh
Enter your score [0-100%] ('q' for quit): -2
Be serious.  Common, try again:
Enter your score [0-100%] ('q' for quit): 120
Be serious.  Common, try again:
Enter your score [0-100%] ('q' for quit): q
Average rating: 0%.
Exiting.