Advertisement
Go-Ice

Lab 2_Shell Script_Ver 2

Sep 26th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.00 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # @author LinChuWen
  4. # course: Operating Systems Lab
  5. # First Version: 2016.09.23
  6. # Last Version:  2016.09.26
  7.  
  8. min=0
  9. max=100
  10. start=$min
  11. end=$max
  12.  
  13. SetTarget(){
  14.     printf "set a target: "
  15.         read target; ans=$target
  16.     Check;
  17. }
  18.  
  19. Check(){
  20.     if [ $ans -le $min ]; then
  21.         echo "WRONG INPUT! your target must greater then $min"
  22.         SetTarget;
  23.     elif [ $ans -ge $max ]; then
  24.         echo "WRONG INPUT! your target must less then $max"
  25.         SetTarget;
  26.     fi
  27. }
  28.  
  29. Hello(){ echo "Welcome to the summoner's rift!"; }
  30. Hello;
  31.  
  32. if [ $1 ]; then
  33.     ans=$1; Check;
  34. else SetTarget; fi
  35.  
  36. while [ $ans -lt $max ]; do
  37.     printf "Guess a number between $start and $end: "
  38.     read input
  39.    
  40.     if [ $input -gt $max ] || [ $input -lt $min ]; then
  41.         echo "WRONG INPUT! range: $min ~ $max"
  42.     elif [ $input -gt $ans ]; then
  43.         if [ $input -lt $end ]; then
  44.             end=$input; fi
  45.     elif [ $input -lt $ans ]; then
  46.         if [ $input -gt $start ]; then
  47.             start=$input; fi
  48.     else
  49.         echo "BINGO! the answer is $ans !"
  50.         break
  51.     fi
  52. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement