Miscellaneous

  Home arrow Miscellaneous arrow Page 3 - The Test in the UNIX Shell
MISCELLANEOUS

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

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

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    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

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap