lua运行带有参数的shell命令不起作用 [英] lua run a shell command with parameters doesn't work
问题描述
我正在尝试从lua脚本执行命令.该命令是简单地运行名为"sha_compare.py"的python脚本.其中的三个参数接受lua脚本中的变量-dady_data和sha:
I am trying to execute a command from lua script. The command is to simply run a python script named "sha_compare.py" of which receives 3 arguments where two of them are variables from the lua script - dady_data and sha:
local method = ngx.var.request_method
local headers = ngx.req.get_headers()
if method == "POST" then
ngx.req.read_body()
local body_data = ngx.req.get_body_data()
local sha = headers['X-Hub-Signature-256']
ngx.print(os.execute("python3 sha_compare.py"..sha..body_data))
else
由于我调用参数的方式,脚本失败.如果我要从cmd运行它,实际的命令应该是这样的:
The script fails because of the way I call the arguments. The actual command if I would have ran it from cmd would have been something like:
python3 python3 sha_compare.py sha256=ffs8df aaaaa
请告诉我如何更改代码以正确地调用带有3个变量的python脚本.
Please tell me how should I change my code to call the python script with 3 vars properly.
如果不可能或很难实现,请告诉我如何调用将接收这三个参数的.sh脚本.
If it is not possible or hard to implement, please let me know how can I call a .sh script which will receive those 3 params.
推荐答案
您没有在参数之间提供空格:您正在尝试执行
You're not providing spaces between the arguments: you're trying to execute
python3 sha_compare.pysha256=ffs8dfaaaaa
执行此操作:
os.execute("python3 sha_compare.py "..sha.." "..body_data)
通常将命令构建为表格并与执行连接起来更为容易:
It's often easier to build the command up as a table, and the concat it for execution:
local cmd = { 'python3', 'sha_compare.py', sha, body_data }
os.execute(table.concat(cmd, " "))
这篇关于lua运行带有参数的shell命令不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!