yanqiufang 发表于 2018-8-22 06:59:14

shell脚本 算平均数

#!/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 ('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."

页: [1]
查看完整版本: shell脚本 算平均数