Flow Control

team bbl


Command execution in a script flows from the first line to the last. However, it's possible to change the order of statement execution. The shell provides two types of commands that change the order of statement execution:

  • Conditional statements: The commands in a conditional statement execute only when a condition is found to be true. Some commands do not execute when the script runs because the condition under which they execute never occurs. By executing statements only when certain conditions are met, the script provides more flexibility to respond to varying conditions. For instance, the script can delete junk files if today is Friday, but not if today is Monday.

    The shell provides two conditional statements:

    • if statement: Tests one or more conditions. Executes commands when a condition is true. You can define sets of alternate commands to execute when none of the conditions are true.

    • case statement: Sets up alternative sections in the case statement, each section containing a set of commands. A pattern is set up for each section. The case statement tests a string. The case statement then executes the commands in the section where the string matches the section pattern.

  • Loops: The commands in a loop repeat. The commands can repeat as many times as needed. For instance, the script can process each file in a directory using the same commands. The commands repeat once for each file. Or, a loop can be set to repeat a number of times, such as 10 times.

    The shell provides three useful looping statements:

    • for loop: The commands in a for loop are set to repeat as long as a condition is true. Often used to set up a loop that repeats a specified number of times.

    • while loop: The commands in a while loop continue to repeat as long as a specified condition is true.

    • until loop: The commands in an until loop continue to repeat until a specified condition is true.

    team bbl



    Spring Into Linux
    Spring Into Linux
    ISBN: 0131853546
    EAN: 2147483647
    Year: 2005
    Pages: 362
    Authors: Janet Valade

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