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


     

    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