if Statements in Scripts

 < Day Day Up > 



In any programming or scripting language, you need to take alternative actions depending on some input. If some condition is true, then execute one set of code, and if it’s not, execute a different set. The following is a generic example of the syntax used:

  if condition   then     condition is zero (true - 0)     execute these commands   else     if condition is not true then     execute these commands   fi
Note 

fi indicates a finished if statement.

Let’s look at a script that illustrates this. In your favorite text editor, enter the following lines and save it as scripteight.sh:

  #!/bin/bash   num=0   echo Please enter an integer   read num   if test $num –gt 0    then     echo $num is positive   else     echo $num is a negative number   fi   exit 0

If you run this script from the shell, you should see something similar to what is displayed in Figure 20.13.

click to expand
Figure 20.13: Using an if statement in a script.

There are several items in this particular script that we need to pay attention to. The first line that is different from what we have previously seen is if test $num –gt 0. This line says to see if the variable num is greater than (gt) zero. If it is, then proceed to the code found under the then statement. If it is not, then proceed to the code found under the else statement. Complete the if with the fi statement.

The if statement is a construct that is common to virtually all scripting and programming languages. Whether you are doing a Linux shell script, a Windows batch file, a JavaScript, or a C++ program, you will have if statements that follow essentially the same structure as you see here. The syntax will be a bit different from one language to the next, but the basic structure is essentially the same.

You also can have an entire if statement, including the fi, inside of another if statement. These are referred to as nested if statements. If you think about this for a moment, it’s not so strange. You use nested if statements in your day-to-day thinking. For example, before going to work in the morning, you might say to yourself that if the freeway is busy, take a side route. Then if the freeway is busy, you might say if alternate side route A is busy, take side route B. A nested if statement, whether it is in a Linux shell script or in your day-to-day thinking, works like this:

  if some condition is true   then    if some other condition is true   then      do action a   else      do action b   else     do action c

Using a nested if inside of a Linux shell script is not much different. Let’s look at an example of a nested if. Type the following code into your favorite text editor and save it as scriptnine.sh.

  #!/bin/bash   age=0   echo Please enter your age   read age   if test $age –gt 16     then       if test $age –gt 18          then         echo You are old enough to vote!         else           echo you are old enough to drive, but not to vote.         fi     else        echo You are too young to drive.   fi   exit 0

If you run this script from the shell, you should see what is shown in Figure 20.14.

click to expand
Figure 20.14: Using nested if statements in a shell script.

You can see that a nested if is not really as complicated as it sounded. The important thing to remember is to complete the inner if statement with an fi. If you do not complete the inner if statement, you will get an error, and your script will not function properly.



 < Day Day Up > 



Moving From Windows to Linux
Moving From Windows To Linux (Charles River Media Networking/Security)
ISBN: 1584502800
EAN: 2147483647
Year: 2004
Pages: 247
Authors: Chuck Easttom

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net