How can I check in Lua if a string is the path to an executable file? It seems that neither the standard library nor, surprisingly, LuaFileSystem provides a way to do this.

有帮助吗?

解决方案

LuaFileSystem has the lfs.attributes() function which returns a table. That, rather perversely, has a key named "mode" which contains a string describing the "type" of node (file, directory, socket, etc).

Although it's not listed in the manual at: http://keplerproject.github.io/luafilesystem/manual.html ... which seems to be the canonical reference for that module ... there is also a 'permissions' key in that table. I think you could parse it for any "x" characters.

I discovered this with:

#!lua 
local lfs = require 'lfs'
attr = lfs.attributes('./some_file')
for name, value in pairs(attr) do
    print (name,value)
    end

其他提示

luaposix has the stat function. It returns a number for the mode field which includes execute permission bits (unlike LuaFileSystem which only gives you the file type.)

There is also access which will check permissions for only the current user.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top