Miscellaneous
  Home arrow Miscellaneous arrow Page 2 - 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 true/false statement


    (Page 2 of 4 )

    If we want to implement a correct decision making mechanism, first we need to determinate what is true and what is false. Every command you run can either succeed or fail for some reason. The command will signal this to the operating system via a returned value. This is the program's exist status.

    This value may also hide the reason for the error. On a command line this will be the value that the last executed command returned. The shell is kind enough to store this value for us in the $? special variable. We can list its value with the help of the echo command.

    touch alfa.txt

    ls alfa.txt

    alfa.txt

    echo $?

    0

    rm alfa.txt

    ls alfa.txt

    ls: alfa.txt: No such file or directory

    echo $?

    2

    You can see that for a successful run, this is zero. When a problem occurred, we got the value two. This may differ depending on the error; however, it will  always be a positive integer number. The error code depends on the command. You can read after this the manual (use the man or help command) for the tool.

    If you search in the manual of ls you will see: “Exit status is 0 if OK, 1 if minor problems, 2 if serious trouble." This logic is somehow inverse to other programming languages. In C/C++, C#, Java, and so forth, a failure is zero and any other value is true. There exists a POSIX rule on what the return value of a program should be. The table below will show you this:

    $? Value

    What does it mean?

    0

    Success

    >0

    Error when interpreting the command. Errors on the redirection, replace of shell variables also enter at this category

    1-125

    The program managed to finish, however it exited with an error

    126

    The command exists, nevertheless we cannot run it

    ( no right to do so, invalid binary file, etcetera)

    127

    The commend does not exist (cannot find it)

    >128

    The program stopped with a signal we sent

    (like Ctrl + Z, Ctrl + C, etcetera)

    The &&, || and ! Structures

    These are the short circuit operators. You can use these in a single command line also. The && will treat the and case, the || will work for the or case while the ! (Negation) will help in using the first two. They were named short circuit operators because their evaluation will stop once the result is obvious.

    Translating, this means that the and will stop after the first false, and the or will stop at the first true result of a command inside a command line structured with these operators. We use them as follows:

    ls alfa.txt && echo “The file was found”

     

    ls alfa.txt || ls beta.txt || ls omega.txt

    This should illustrate how they work. The first will print out “The file was found” message only if ls managed to find it. The second one will print out the data of either the alfa.txt, beta.txt or omega.txt. However, only the first one of them was found. We can also combine them to create a simple if-then-else structure.

    ls alfa.txt && echo “The file was found” || echo “There exists no such file”

    The negation mark will negate the result of a command. If that was true, it will turn it to false, and vice versa. The negation will also have as an effect that, inside the $? we will have a negated value. For instance:

    ! ls alfa.txt

    echo $0

    1

    In the upper scenario, there was an alfa.txt. The ls command returned zero, however it was immediately negated by the exclamation mark and this negated value is inside the $? special variable.

    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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek