Miscellaneous

  Home arrow Miscellaneous arrow Page 2 - 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 - Files and strings


    (Page 2 of 4 )

    We check traits for the files quite often. We want to find out if a file exists, if we have the right to delete it or even if there is a valid, executable file. These tasks become simple with the syntaxes I am going to show you. The only question is what the syntax of the expression is. After this, you can replace that with either of the syntaxes of the test presented on the previous page. For the file it can be:

    Expression

    Test checks for what trait?

    -d file

    True if the file exists and it is a directory (d)

    -e file

    True if the file exists (e)

    -f file

    True if the file exists and is a regular file (f)

    -L file

    True if the file exists and it is a symbolic link (L)

    -r file

    True if the file exists and you can read (r) it

    -s file

    True if the file exists and its size (s) is greater than zero

    -w file

    True if the file exists and you can write (w) to it

    -x file

    True if the file exists and you can execute (x) it

    -O file

    True if the file exists and it its owner (O) is the actual user

    -G file

    True if the file exits and its owner is in your group (g)

    File1 -nt file2

    True if File1 is newer than (nt) file2 (from the point of view of the access time)

    File1 -ot file2

    True if File1 is older than (ot) file2

    File1 -ef file2

    True if the two files are equal files (ef). For this, the i-node and device numbers must be the same.

    A couple of examples:

    #check if we can run the first argument of a shell script

    if test -x $1

    then

    echo " We can and will start the program"

    bash $1

    fi

    #if alfa.txt exists and we can delete it, remove it

    if [ -e alfa.txt ] && [ -w alfa.txt ]

    then

    rm alfa.txt

    else

    echo "The file does not exists or no right to delete it"

    fi

    Then again, there we have the expressions related to strings.

    Expression

    Test checks for what trait?

    -z string

    True if the string has a length equal to zero (z)

    -n string

    True if the length of the string is not (n) zero

    string1 = string2

    True if the strings are the same

    string1 != string2

    True if the strings differ

    The usage of this is just as simple. Let us consider the following script snippets, where the $1 is the first argument of the script:

    if test -z alfa

    then

    echo The variables alfa has a length equal with zero

    fi

    if [ $1 = "Yeti" ]

    then

    echo The first argument is Yeti.

    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 7 - Follow our Sitemap