While Loop
Linux Commands - Shells

While Loop
The while loop will continue until a specific requirement is met.  As long as the exit status is zero, the commands will loop down until the done is reached and loop back through.  Exit will occur when a nonzero occurs.

Lesson 12 | Lesson 14

In the following script, as long as the user does not type Colts the script will loop forever.  

The variable must be declared to be able to function that way you want, so this line starts with the variable "Rams", which really does nothing but help you declare the value of the variable.

INPUT_STRING=Rams

The "!" is the same as saying not.  So this line tells the script that as long as the input is not Colts it should continue.

while [ "$INPUT_STRING" != "Colts" ]

The read INPUT_STRING will take the input placed on the command line and check that input against the "Colts" line.

#!/bin/sh
#
INPUT_STRING=Rams
while [ "$INPUT_STRING" != "Colts" ]
do
echo "What is the best football team (Type Colts to quit)"
read INPUT_STRING
echo "You guessed: $INPUT_STRING"
done

 

 


Copyright CyberMontana Inc. and BeginLinux.com
All rights reserved. Cannot be reproduced without written permission. Box 1262 Trout Creek, MT 59874