火炬,如何用"dofile"和输入参数执行脚本?

Dav*_*.it 8 shell lua torch

我正在使用th命令从我的Linux shell执行Torch脚本.这个Torch脚本有两个输入参数:

th torch_script.lua input_parameter1 input_parameter2

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

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


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

external_command.lua内容:

local arg = arg or {...} 
input_parameter = arg[1]
print("input_parameter ".. input_parameter);
Run Code Online (Sandbox Code Playgroud)

在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  
Run Code Online (Sandbox Code Playgroud)

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

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

function runfile(scriptName, input)
  opt = nil
  arg = input
  dofile(scriptName)
end
Run Code Online (Sandbox Code Playgroud)

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

th> scriptName = "external_command.lua"
th> require './runfile.lua'
th> runfile(scriptName, "Hello world");
Run Code Online (Sandbox Code Playgroud)

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

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  
Run Code Online (Sandbox Code Playgroud)

hjp*_*r92 0

你用loadfile

local TempFunc = loadfile "torch_script.lua"
TempFunc(input_parameter1, input_parameter2)
Run Code Online (Sandbox Code Playgroud)