UNIX & Linux Shell Scripting Tutorial
The while statement is used when you want to loop while a statement is true. This is the same in many other programming and scripting
languages. The body of the loop is put between do and done. Suppose that we want to write a script to have the user guess what
number we are thinking of. The best way to use this would be to use a while loop.
#!/bin/sh # Guess the number game. ANSWER=5 # The correct answer CORRECT=false # The correct flag while [ "$CORRECT" != "true" ] do # Ask the user for the number... echo "Guess a number between 1 and 10. " read NUM # Validate the input... if [ "$NUM" -lt 1 ] || [ "$NUM" -gt 10 ]; then echo "The number must be between 1 and 10!" elif [ "$NUM" -eq "$ANSWER" ]; then echo "You got the answer correct!" CORRECT=true else echo "Sorry, incorrect." fi done
You can also loop while reading a variable to make the code simpler. Let's try the above example revised:
#!/bin/sh # Guess the number game. Version 2.0 ANSWER=5 # The correct answer echo "Guess a number between 1 and 10. " while read NUM do # Validate the input... if [ "$NUM" -lt 1 ] || [ "$NUM" -gt 10 ]; then echo "The number must be between 1 and 10! Guess again. " elif [ "$NUM" -eq "$ANSWER" ]; then echo "You got the answer correct!" exit else echo "Incorrect, guess again. " fi done
Another way to loop forever is to use the : in your while statement. Let's write a simple program that will
print out how long it has been running until the user presses Ctrl+C to terminate the program.
#!/bin/sh COUNTER=0 while : do sleep 1 COUNTER=`expr $COUNTER + 1` echo "Program has been running for $COUNTER seconds..." done
This program will loop until the user presses Ctrl+C. Notice that we use something else new here. The unix expr command is used to evaluate a mathematical expression. If you want to increment a variable, this is the command that you will use.
The for statement is used when you want to loop through a list of items. The body of the loop is put between do and done.
Let's say that we want to write a program that will validate numbers in a given list. These numbers can be loaded from a file, hard coded, or
manually entered by the user. For our example, we will ask the user for a list of numbers separated with spaces. We will validate each number
and make sure that it is between 1 and 100. The best way to write a program like this would be to use a for loop.
#!/bin/sh # Validate numbers... echo "Please enter a list of numbers between 1 and 100. " read NUMBERS for NUM in $NUMBERS do if [ "$NUM" -lt 1 ] || [ "$NUM" -gt 100 ]; then echo "Invalid Number ($NUM) - Must be between 1 and 100!" else echo "$NUM is valid." fi done
Notice that we give the for statement a variable "NUM", this can be whatever variable name you want to use. It will loop through each item in the given variable (in this case $NUMBERS) and put that item in the $NUM variable. Run the file and give it the input "1 4 3 55 48 120 1000 4 1" and you will have the following output:
Please enter a list of numbers between 1 and 100. 1 4 3 55 48 120 1000 4 1 1 is valid. 4 is valid. 3 is valid. 55 is valid. 48 is valid. Invalid Number (120) - Must be between 1 and 100! Invalid Number (1000) - Must be between 1 and 100! 4 is valid. 1 is valid.
Like Progressive Metal? Check out my band on Spotify, please follow us!
Blob Entry 1
Blob Entry 2
Blob Entry 3
Blob Entry 4
Blob Entry 5
Blob Entry 6