Using while and if


Using while and if

If you want to build more complex scripts, you can use other statements to control your automations. You can turn to conditional statements that allow you to activate certain sections of your script only when certain conditions apply. To use conditional statements, you need to create a condition for the statement to test. This lesson covers the basics; just be aware that this can get in-depth as there are many operators, and different items of syntax you can work with.

Hello Operator? Almost all programming languages in existence have a set of operators that perform arithmetical operations. Computers are mathematical devices, which function based on Boolean math.


The if command works in much the same way as the while command, except it doesn't loop; it simply executes one command based on the condition. The syntax of the if command is as follows: if ( <condition> ) <command>. The while command, with the syntax while (<condition>), does things while a certain condition holds.

To set up a conditional statement, you can check out the following syntax, which is based on if and evaluates an expression placed in parentheses as seen here:

 if ( the expression ) actionA [else actionB] 

If the expression evaluates as true, then the first action (actionA) is done. The other clause you can specify is else, which makes sense when you say it verbally outside of typing it in the Unix shell prompt.

"If you don't have the key, then you don't get in." Easy enough, right?

  • actionA: lost key

  • actionB: no enter

That should wrap up the if statement for you. Again, this can get more complex, but for this lesson, you should feel comfortable with using the if statement when building a shell script.



    SAMS Teach Yourself Unix in 10 Minutes
    Sams Teach Yourself Unix in 10 Minutes (2nd Edition)
    ISBN: 0672327643
    EAN: 2147483647
    Year: 2005
    Pages: 170

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