I'm executing a Torch script from my Linux shell, with the th
command. This Torch script takes two input parameters:
th torch_script.lua input_parameter1 input_parameter2
Now I'd like to run this script through the Torch shell. To do this, I have to use the dofile
command. But in this case, I don't know how to pass the input parameters input_parameter1
and input_parameter2
.
In Torch, how to pass some input parameters to the dofile
execution command?
EDIT: Here's the code that I'm trying to run. I'm not able to run it properly, maybe you can tell me why
external_command.lua content:
local arg = arg or {...}
input_parameter = arg[1]
print("input_parameter ".. input_parameter);
On the 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
EDIT 2: I tried the solution posted by TonyHsu, but it does not work anyway. Here's what I'm doing.
I define a function runfile()
in a script called runfile.lua
which contains the following code:
function runfile(scriptName, input)
opt = nil
arg = input
dofile(scriptName)
end
Then I use the external_command.lua
script that I previously defined as scriptName
input parameter for this function:
th> scriptName = "external_command.lua"
th> require './runfile.lua'
th> runfile(scriptName, "Hello world");
Unfortunately, also in this case I get an error:
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
I think the trick is to pass the parameters in global variable 'args'. Say, I have the following content in 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
Then define runfile() as follows.
function runfile(f, ...)
local tmp = args -- save the original global args
args = table.pack(...)
dofile(f)
args = tmp -- restore args
end
I've tested it in th. It looks like this.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With