Miscellaneous
  Home arrow Miscellaneous arrow Page 3 - Control Mechanisms of the UNIX Shell
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS

Control Mechanisms of the UNIX Shell
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2009-05-06

    Table of Contents:
  • Control Mechanisms of the UNIX Shell
  • The true/false statement
  • The if control statement
  • The expr command

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Control Mechanisms of the UNIX Shell - The if control statement


    (Page 3 of 4 )

    For the decision control we use the “if structure.” This will assure that, depending upon a condition, a command line will or will not be executed. Of course, there will also be an alternative branch containing what should be done if the assumption is false. The syntax for simple decision making is:

    if condition_command_line

    then

    #commands

    else

    #commands

    fi

    We can cascade this one into another with the following syntax:

    if condition_command_line

    then

    #commands

    elif condition_command_line

    #commands

    else

    #commands

    fi

    The conditions of the command line can be many. It can be a text expression with a command like the expr and the test. This we will cover later on. It can be a command line composed of multiple commands with the help of pipes or the upper &&,||. ! structures. If this is the case, after the if, it will test the $? special variable to determine whether or not the command was correctly executed.  Before this, it will execute any command standing in the condition_command_line.

    This leads to the fact that we can also use it with no conditions, just a command line. In this case it will just test the current value of the $?. What you should also know is that the shell will not allow any empty branch for the “if structure.” If you created a branch, you must do something there. If for some reason you still have nothing to do, call the empty command with the “:” symbol. The example below will show you how to do this:

    if cd alfa

    then

    echo “The directory alfa exits”

    else

    :

    fi

    #or

    cd alfa

    if expr $? = 0

    then

    echo “The directory alfa exits”

    else

    :

    fi

    Most of the time we will use the test command to help us decide a condition. This is a very important command, and very useful; therefore, I have decided to accord to it an entire future article. In it, I will cover in detail how to use it along with the “if structure.”

    More Miscellaneous Articles
    More By Gabor Bernat


     

    MISCELLANEOUS ARTICLES

    - Using PHP to Stream MP3 Files and Prevent Il...
    - 10 Must Have Firefox Improvements
    - All About OpenOffice 3.0
    - Shell Script Writing
    - Loops in the UNIX Shell
    - The Test in the UNIX Shell
    - Data Streams and the UNIX Shell
    - Control Mechanisms of the UNIX Shell
    - Variables Within the UNIX Shell
    - The Shell and UNIX
    - In Detail: UNIX File Systems
    - Rights Management in UNIX
    - UNIX File Systems
    - The Terminal in UNIX
    - Operating Systems and UNIX





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek