101 shell script guessing number game code _linux shell

Source: Internet
Author: User

The original book This example is to teach debugging script, it deliberately gave a few errors in the script, teach us to understand the system prompts the error, and modify the run. But it's more difficult to write, so put the modified script up, and the script is more interesting

#!/bin/sh
 # Hilow--A simple number-guessing game

 biggest=100              # Maximum number possible
 guess=0                # Guessed by player
 Guesses=0               # Number of guesses made
 number=$ ($$% $biggest))       # random number, between 1 and $biggest while

 [$guess-ne $number]
  do echo-n "guess"; read guess
  if [$guess-lt $number ] ; Then
   echo "... bigger!"
  elif ["$guess"-gt $number]; Then
   echo "... smaller!i"
  fi
  guesses=$ (($guesses + 1)) Done
 echo "right!! Guessed $number in $guesses guesses. "
 Exit 0

Scripting analysis

#1) number=$ (($$% $biggest)) $$ represents the PID that executes this shell because each execution of the shell's PID is not the same, so the number is 1-100 random.

#2) $guess-ne $number, if ["$guess"-lt $number],elif ["$guess"-gt $number]; compare the numbers entered with the $number.

#3) The entire script is not complex, but the logic is clear and interesting.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.