Re: Off Topic Scripting Question

From: Steve Thornhill (steve_thornhill@UK.IBM.COM)
Date: Fri Jul 19 2002 - 10:09:12 EDT


Excerpt from page 131 of O'reilly's Learning the Korn Shell

Operator True if...
-a file file exists
-d file file is a directory
-f file file is a regular file (i.e., not a directory or
other special type of file)
-r file You have read permission on file
-s file file exists and is not empty
-w file You have write permission on file

..... + a few more.

Any help?

Steve Thornhill - IBM CLS
Int - 255104
Ext - 023 92 565104
Mob - 07778 289770
Email/Sametime - steve_thornhill@uk.ibm.com

                      Bill Thompson
                      <bill. To: aix-l@Princeton.EDU
                      thompson@GOODYEAR cc:
                      .COM> Subject: Off Topic Scripting Question
                      Sent by: IBM AIX
                      Discussion List
                      <aix-l@Princeton.
                      EDU>

                      19/07/2002 14:38
                      Please respond to
                      IBM AIX
                      Discussion List

This is a Borne/Korn shell question which is not AIX specific. Sorry if
it's somewhat off topic.

I was reviewing an old script and found the following conditional:

if [ -a $filename ]; then ...

My immediate reaction is that the -a should have been a -f

While the -f is probably the more appropriate test, I did some
experimenting at the command line...

if [ -a ]; then echo true; else echo false; fi
true

if [ -a .profile ]; then echo true; else echo false; fi
true

if [ -a not_a_file ]; then echo true; else echo false; fi
false

-a is the binary AND which is typically used between two conditionals like
this:
    if [ $A -lt $B -a $C = "OK" ]; then ...

Assuming the contents of "$filename" is the name of a file (or directory),
can somebody explain why "if [ -a $filename ]" returns TRUE if the file
exists and FALSE if the file does not exist?

BTW:
if [ .profile ] or if [ not_a_file ] both return TRUE (which is what I
would expect because the strings are not null).

TIA

Bill Thompson
The Goodyear Tire & Rubber Company
Contains Confidential and/or Proprietary Information.
May Not Be Copied or Disseminated Without Express Consent of The Goodyear
Tire & Rubber Company.



graycol.gif
ecblank.gif
pic19823.gif

This archive was generated by hypermail 2.1.7 : Wed Apr 09 2008 - 22:16:04 EDT