Shell Torch,如何使用“执行脚本”;dofile";和输入参数?

Shell Torch,如何使用“执行脚本”;dofile";和输入参数?,shell,lua,torch,Shell,Lua,Torch,我正在使用th命令从Linux shell执行Torch脚本。此Torch脚本采用两个输入参数: th torch\u script.lua输入参数1输入参数2 现在我想通过火炬外壳运行这个脚本。为此,我必须使用dofile命令。但是在这种情况下,我不知道如何传递输入参数input\u参数1和input\u参数2 在Torch中,如何将一些输入参数传递给dofile执行命令 编辑:以下是我试图运行的代码。我不能正常运行,也许你能告诉我原因 外部_命令.lua内容: local arg = ar

我正在使用
th
命令从Linux shell执行Torch脚本。此Torch脚本采用两个输入参数:

th torch\u script.lua输入参数1输入参数2

现在我想通过火炬外壳运行这个脚本。为此,我必须使用
dofile
命令。但是在这种情况下,我不知道如何传递输入参数
input\u参数1
input\u参数2

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


编辑:以下是我试图运行的代码。我不能正常运行,也许你能告诉我原因

外部_命令.lua内容:

local arg = arg or {...} 
input_parameter = arg[1]
print("input_parameter ".. input_parameter);
在外壳上:

$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
然后,我使用
external_命令.lua
脚本,我之前为该函数定义了
scriptName
输入参数:

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  

您可以使用
loadfile

local TempFunc = loadfile "torch_script.lua"
TempFunc(input_parameter1, input_parameter2)

也许您可以先尝试定义一个函数:

function runfile(<scriptName>, ...)
opt = nil
arg = {...}
dofile(<scriptName>)
end
函数运行文件(,…)
opt=nil
arg={…}
dofile()
结束
然后跑

runfile(<scriptName>,...)
runfile(,…)

我认为关键在于在全局变量“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]

谢谢,但这对我不起作用。
输入参数1
通过
chromSel=arg[1]
命令传递,但此赋值不起作用:
错误:尝试连接全局“chromSel”(零值)
。为什么?在
torch\u script.lua的开头添加
local arg=arg或{…}
,谢谢。我将该行添加到文件的#1行,但它不起作用。它会生成相同的错误使用
loadfile
而不是
load
谢谢@EgorSkriptunoff,但它的工作原理不同。同样的错误…嘿,有人能帮忙吗?可能是
runfile(脚本名,{“Hello world”})?嘿@EgorSkriptunoff你说得对!如果你想重写它作为一个答案,我会接受它作为正式答案。谢谢TonyHsu,但它无论如何都不起作用。我编辑了我的问题,报告了我考试的细节;你能看一下吗?