Miscellaneous
  Home arrow Miscellaneous arrow Page 3 - The Test in 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

The Test in the UNIX Shell
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2009-05-20

    Table of Contents:
  • The Test in the UNIX Shell
  • Files and strings
  • Logical expressions and numbers
  • The Case

  • 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


    The Test in the UNIX Shell - Logical expressions and numbers


    (Page 3 of 4 )

    We can also link together logical expressions with either the && or || operators I introduced in my previous article, or leave this also to the test command. In this case, the expression will look as follows in the table:

    Expression

    Test checks for what trait?

    ! expression

    True if the expression is false

    expression_1 -a expression_2

    True if both of the expressions are true; it's a logical and (-a). 

    expression_1 -o expression_2

    True if either one of the expressions is true; it's a logical or (-o).

    For example:

    #If alfa is not a directory signal to the user

    if test ! -d alfa 2>/dev/null

    then

    echo "alfa is not a directory Invalid input"

    fi

    if [ [ -f alfa ] -a [ -O alfa ] ]

    then

    echo The alfa is a regular file and we own it

    fi

    Inside the shell variables, everything is stored as a string. Nevertheless, these characters can also be only digits and form a number. When we have more than one of them, we can start to make some basic comparisons. The numbers can also be negative; however, only integer numbers are accepted.

    Expression

    Test checks for what trait?

    Arg1 -eq Arg2

    True if Arg1 is equal (eq) to Arg2

    Arg1 -ne Arg2

    True if Arg1 is not equal (ne) to Arg2

    Arg1 -lt Arg2

    True if Arg1 is less than (lt) Arg2

    Arg1 -le Arg2

    True if Arg1 is less than or equal (le) to Arg2

    Arg1 -gt Arg2

    True if Arg1 is greater than (gt) Arg2

    Arg1 -ge Arg2

    True if Arg1 is greater than or equal (ge) to Arg2

    The argument can be also beside the number for the -l string special expression when the length of the string will replace this. Running a test will also save the result of the test into the $? special variable. Here are a couple of instances of this case:

    if [ $1 -ne 2 ]

    then

    echo The first argument is not two

    fi

    if test $2 -eq 0

    then

    echo The second argument is zero

    fi

    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