加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell – Torch,如何用“dofile”和输入参数执行脚本?

发布时间:2020-12-16 01:14:45 所属栏目:安全 来源:网络整理
导读:我正在使用th命令从我的 Linux shell执行Torch脚本.这个Torch脚本有两个输入参数: th torch_script.lua input_parameter1 input_parameter2 现在我想通过Torch shell运行这个脚本.为此,我必须使用dofile命令.但在这种情况下,我不知道如何传递输入参数input_
我正在使用th命令从我的 Linux shell执行Torch脚本.这个Torch脚本有两个输入参数:

th torch_script.lua input_parameter1 input_parameter2

现在我想通过Torch shell运行这个脚本.为此,我必须使用dofile命令.但在这种情况下,我不知道如何传递输入参数input_parameter1和input_parameter2.

在Torch中,如何将一些输入参数传递给dofile执行命令?

编辑:这是我正在尝试运行的代码.我无法正常运行,也许你可以告诉我原因

external_command.lua内容:

local arg = arg or {...} 
input_parameter = arg[1]
print("input_parameter ".. input_parameter);

在shell上:

$th
th> tempFunc = load "external_command.lua"
th> tempFunc("try")
[string "_RESULT={tempFunc("try")}"]:1: attempt to call global 'tempFunc' (a nil value)
stack traceback:
    [string "_RESULT={tempFunc("try")}"]:1: in main chunk
    [C]: in function 'xpcall'
    /home/davide/torch/install/share/lua/5.1/trepl/init.lua:630: in function 'repl'
    ...vide/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:185: in main chunk
    [C]: at 0x004064d0

编辑2:我尝试了TonyHsu发布的解决方案,但无论如何它都无法正常工作.
这就是我正在做的事情.

我在名为runfile.lua的脚本中定义了一个函数runfile(),其中包含以下代码:

function runfile(scriptName,input)
  opt = nil
  arg = input
  dofile(scriptName)
end

然后我使用我之前为此函数定义为scriptName输入参数的external_command.lua脚本:

th> scriptName = "external_command.lua"
th> require './runfile.lua'
th> runfile(scriptName,"Hello world");

不幸的是,在这种情况下,我收到一个错误:

external_command.lua:4: attempt to concatenate global 'input_parameter' (a nil value)
stack traceback:
    external_command.lua:4: in main chunk
    [C]: in function 'dofile'
    /home/davide/torch/temp/runfile.lua:4: in function 'runfile'
    [string "runfile(scriptName,"Hello world");"]:1: in main chunk
    [C]: in function 'xpcall'
    /home/davide/torch/install/share/lua/5.1/trepl/init.lua:648: in function 'repl'
    ...vide/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:185: in main chunk
[C]: at 0x004064d0
我认为诀窍是传递全局变量’args’中的参数.说,我在external_command.lua中有以下内容.
-- args has been set by the caller
if not args or #args == 0 then
    print('no input_parameter')
else
    print('#args = ' .. #args,'input_parameter: ' .. args[1])
end

然后按如下方式定义runfile().

function runfile(f,...)
    local tmp = args  -- save the original global args
    args = table.pack(...)
    dofile(f)
    args = tmp  -- restore args
end

我已经测试了它.看起来像这样.

th> runfile('ext_command.lua',10)
#args = 1       input_parameter: 10
                                                                      [0.0002s]
th> runfile('ext_command.lua','a','b','c')
#args = 3       input_parameter: a
                                                                      [0.0002s]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读