shel while循环示例小结

  [root@openEuler ~]# cat while4.sh

  #!/bin/bash

  price=$[ $RANDOM % 100 ]

  time=0

  while true

  do

  read -p 'Please enter product price [0-99]: ' input

  let time++

  if [ $input -eq $price ]; then

  echo 'Good luck, you guessed it.'

  echo 'You have guessed $time times.'

  exit 0

  elif [ $input -gt $price ]; then

  echo "$input is to high"

  else

  echo "$input is to low"

  fi

  if [ $time -eq 5 ]; then

  echo "You have guessed is 5 times. exit"

  exit 1

  fi

  done

  [root@openEuler ~]# bash while4.sh

  Please enter product price [0-99]: 50

  50 is to low

  Please enter product price [0-99]: 80

  80 is to high

  Please enter product price [0-99]: 70

  70 is to high

  Please enter product price [0-99]: 60

  60 is to low

  Please enter product price [0-99]: 65

  65 is to low

  You have guessed is 5 times. exit

  [root@openEuler ~]#