Flow Control

Flow
The order in which you set up commands to execute s called flow.  This essentially sets commands to act the same each time they execute.  However, there will be times when you want to change the way the commands execute based on different situations, thus change the flow.  Changing the flow can be done by the “if statement” or the  “case statement”.

Lesson 10 | Lesson 12

The if statement performs different actions depending upon if a certain condition is true or false.  Here is an example of an if...then clause.

log_errors
This is a simple script to evaluate the level of problems that your web server is experiencing.  This is not a practical
script but it is a good way to do some practice and see how things work.
########################################
#!/bin/bash
# Practice: Error Logs

errors=`cat /var/log/httpd/error_log | wc -l`


if (( errors >= 0 && errors <= 250 ))
then
echo "When you get time check your web error logs!"
elif (( errors >= 251 && errors <= 500 ))
then
echo "Why don't you fix some web site errors?"
elif (( errors >= 501 && errors <= 1000 ))
then
echo "Dude...you are developing some issues"
elif (( errors >= 1001 && <= 100000 ))
then
echo "DANGER...DANGER...YOU HAVE GOT PROBLEMS!"
fi
#########################################

The tool used to evaluate the level of problems is wc and the option "-l" will list the number of lines, thus the number of reported errors, in the /var/log/httpd/error_log.  This log is typically rotated once a week so it could potentially have quite a few errors.  You will need to change the location for the log you want to check based on your Linux distribution.  The output and any errors in checking the log are sent to the garbage.


wc -l /var/log/httpd/error_log > /dev/null 2>&1



The if..then clause starts and it contains a double requirement(&&).  The variable errors is evaluated and if it has 0 or more errors AND if it has less or equal to 250 errors, then a specific line of text appears.


if ((errors >= 0 && errors <= 250))

The elif provides for the situation where the evaluation does not fit so it keeps evaluating until there is a match.  The if...then clause is ended with "fi".

 


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