You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
th> local sh = require('sh')
[0.0001s]
th> sh
function: 0x410888b0
[0.0001s]
th> local dockerbusybox = sh.command('docker', 'run', 'busybox')
[string "local dockerbusybox = sh.command('docker', 'r..."]:1: attempt to index global 'sh' (a function value)
stack traceback:
[string "local dockerbusybox = sh.command('docker', 'r..."]:1: in main chunk
[C]: in function 'xpcall'
/home/tao/Datas/torch/install/share/lua/5.1/trepl/init.lua:679: in function 'repl'
...atas/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:204: in main chunk
[C]: at 0x004065c0
[0.0002s]
th> dockerbusybox('echo', 'hello')
sh: 1: dockerbusybox: not found
[0.0027s]
It's because of how lua variables are looked up in the lua shell vs during full script interpretation; so these two things will produce different results.
test.lua:
#!/usr/bin/lua
local sh = require("sh")
print(type(sh))
$ lua test.lua
table
$ lua
Lua 5.2.4 Copyright (C) 1994-2015 Lua.org, PUC-Rio
> local sh = require("sh")
> print(type(sh))
function
And the reason this happens is because luash changes _G's metatable, adding a __index that translates all attempts at accessing unset _G.xyz members as invoking a shell command of the same name (xyz).
So in your shell, type(sh) == "function" is not a defined function from the library, it's a function that in turn would have invoked this shell command for you: $ sh
local mt = getmetatable(_G)
if mt == nil then
mt = {}
setmetatable(_G, mt)
end
-- set hook for undefined variables
mt.__index = function(t, cmd)
return command(cmd)
end
When I installed the sh as the command
It's successful , but when I run the example as above , the problem occured .
I don't know how to solve it, please help me sovle this problem. Thanks in advance ~ : )
The text was updated successfully, but these errors were encountered: