Miscellaneous
  Home arrow Miscellaneous arrow Page 4 - 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 expr command


    (Page 4 of 4 )

    When we evaluate variables, sometimes we would like to perform some basic arithmetic operations. From this, we will learn in the end whether or not a condition is active. The simplest command what deals with this is the expr. The expr can also perform some basic string operations.

    The syntax of the expr is quite simple:

    expr expression

    The expression will print the result on the standard output. What can the expression be? The table below shows a list of these:

    Expression

    What does it do?

    ARG1 | ARG2

    ARG1 if that is not zero, otherwise ARG2

    ARG1 & ARG2

    ARG1 if neither is zero or zero length string, otherwise 0

    ARG1 < ARG2

    ARG1 is smaller than ARG2

    ARG1 <= ARG2

    ARG1 is smaller or equal than ARG2

    ARG1 = ARG2

    ARG1 is equal with ARG2

    ARG1 != ARG2

    ARG1 is not equal with ARG2

    ARG1 >= ARG2

    ARG1 is greater or equal than ARG2

    ARG1 > ARG2

    ARG1 is greater than ARG2

    ARG1 + ARG2

    Arithmetic sum

    ARG1 - ARG2

    Arithmetic subtraction

    ARG1 * ARG2

    Arithmetic multiplication

    ARG1 / ARG2

    Arithmetic divide

    ARG1 % ARG2

    Arithmetic modulo of ARG1/ARG2

    STRING : REGEXP

    string match from the first character of the STRING

    match STRING REGEXP

    The same as above

    substr STRING POS LENGTH

    returns from STRING a substring that start from POS and has a length of LENGTH

    index STRING CHARS

    Returns the index from STRING where any characters from CHARS can be found

    length STRING

    The length of the STRING

    You have to bypass the meta-characters with the help of the backslash. You have to do so with the <,>, &, |, * ones.

    A couple of examples:

    expr 1 + 2

    a= $( expr 2 > 3)

    b= `expr 1 * 2`

    You need to leave the spaces between the arguments and the operators. Otherwise, you will wake up with a beautiful error message. Here is how you can use the expr with the “if structure:”

    if expr 2 < 3

    then

    echo Two is smaller than three

    fi

    This will be all for today. Next week we will cover data streams, and then we will learn a more advanced and complicated command to test conditions. This is the test command. Until then, start up your console, make sure you try out what you learned today, and remember that repetition is the mother of knowledge. Thank you for reading my article. Please rate it. This will help further readers to get a more realistic picture of it. If you are unclear on anything, do not hesitate to ask your questions on the blog. Live With Passion!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    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