lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Can anybody explain why this Lua script (which is in the working directory as testr with the executable flag set):

#!/usr/local/bin/lua
print('Is there a shell?  ', os.execute())
print('Check for a non existent file gives:', os.execute('ls NOSUCHFILE '))


Would give these results when called inside the interpreter:

> dofile'testr'
Is there a shell?   true
ls: NOSUCHFILE: No such file or directory
Check for a non existent file gives: nil exit 1

>
> os.execute'testr'
Is there a shell?   1
ls: NOSUCHFILE: No such file or directory
Check for a non existent file gives: 256


when called directly from the command line it also gives:

Joses-MacBook-Pro:luawork jtorrebueno$ testr
Is there a shell?   1
ls: NOSUCHFILE: No such file or directory
Check for a non existent file gives: 256


I thought checking the return from os.execute alone would be enough to tell if a file exists because the return from os.execute is nil if ls fails but of course 256 is true in Lua
It seems as though os.execute behaves differently in the interpreter and when called in a stand alone program.

All this is under OSX with the bash shell BTW.

Thanks

Jose Torre-Bueno Ph.D.
Empowered Energy Solutions
(858) 292-6100 
(619) 977-0553 (cell)
CA Contractor B. 932346
jtorrebueno@empoweredenergysolutions.com
www.empoweredenergysolutions.com