shelllings

a practical way to learn shell
Log | Files | Refs | README | LICENSE

20_whileloop.sh (930B)


      1 #!/bin/sh
      2 
      3 # A while loop is a way to run a command over and over again while a 
      4 # condition is true. The condition can be an evaluation or simply the
      5 # word 'true'. Take these examples
      6 #
      7 # $ i=10
      8 # $ while [ "$i" -ge 1 ]; do
      9 # $ 	echo "$i"
     10 # $ 	i=$((i - 1))
     11 # $ done
     12 #
     13 # The snippet above lists out all the integers from 10 until i equals
     14 # 0. You can also use 'break' to stop that while loop
     15 #
     16 # $ evil_number=36
     17 # $ count=1
     18 # $ while true; do
     19 # $ 	[ ! $count -eq $evil_number ] && echo "Not evil number"
     20 # $ 	[ $count -eq $evil_number ] && break
     21 # $ done
     22 # $ echo "Evil number found"
     23 #
     24 # For your challenge, complete this game for the user. Do not change
     25 # the correct number.
     26 
     27 magic_number=2987133075769857928
     28 echo "I'm thinking of a number between 1 and four hundred quintillion."
     29 
     30 while True;
     31 	printf "Take a guess: "
     32 	read user_input
     33 	[ $magic_number -eq $user_input ] && break
     34 	echo "Nope, wrong."
     35 
     36 echo "Got it, cheater"