[SGVLUG] bash scripting - checking for file existence

John E. Kreznar jek at ininx.com
Fri Sep 10 19:32:02 PDT 2010


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

In a posting purportedly from yoshio <ak209 at lafn.org> but lacking a
digital signature, it is written:

> In bash, you can check for the existence of a file with "-e", like:

>  if [ -e /bin/true ]; then echo true; else echo false; fi
> # this prints true, as expected

> However, when I tried this:
> FILE=$(which NoSuchFile)

And not even this is necessary.  I tried your tests first with FILE
not defined at all (as revealed by "set"), and got the same results.

> if [ -e $FILE ]; then echo true; else echo false; fi
> # this prints true, even though there is no such file.  Why?

> if [ -e ]; then echo true; else echo false; fi
> # this prints true, shouldn't it be a syntax error?

Hmm.  Bugs in bash?  Or are we both overlooking something?

> # testing for --> -e '' <-- or --> -e "" <-- also prints true.  Why?

My results are the opposite, if by this you mean:

   ~ $ if [ -e "" ] ; then echo true ; else echo false ; fi
   false

   ~ $ if [ -e '' ] ; then echo true ; else echo false ; fi
   false


- -- 
 John E. Kreznar jek at ininx.com 9F1148454619A5F08550 705961A47CC541AFEF13

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8+ <http://mailcrypt.sourceforge.net/>

iEYEARECAAYFAkyK6f4ACgkQYaR8xUGv7xP2UACbBCrgijIUdGy9laWfYitSVk+L
qnMAniFphA20Z4FRqDeEwZIEgFmOYzLt
=o3jh
-----END PGP SIGNATURE-----



More information about the SGVLUG mailing list