Re: Off Topic Scripting Question

From: Jean-Marc Monnez (monnez.jean-marc@AGORA.MSA.FR)
Date: Fri Jul 19 2002 - 10:10:37 EDT


test -a $filename returns true if $filename exists (file, device, pipe, link)
test -f $filename returns true if $filename exists and is a regular file

There may be a syntax confusion with the AND -a operator. In most cases, the
shell interpretation knows syntaxically if it is a test operator or a AND
operator.

Regards

-- JMM

Bill Thompson wrote :

> 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.



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