os.execute变量

我有多个Steam帐户,希望通过单个Lua脚本以我指定的选项启动这些帐户。我几乎已经把所有东西都整理好了,除了使用提供的代码进行启动。我不知道如何使用这种格式“传递”变量。

function Steam(n, opt1, opt2, opt3)
os.execute[["start C:\Program" "Files\Sandboxie\Start.exe /box:Steam2 D:\Steam\steam.exe -login username password -opt1 -opt2 -opt3"]]
end

我已经设置了我的用户名和沙箱,以便仅需要更改数字(fenriros2,fenriros3,Steam2,Steam3等),密码相同。

基本上,我想要这个;

Steam(3, -tf, -exit, -textmode)

做;

os.execute[["start C:\Program" "Files\Sandboxie\Start.exe /box:Steam3 D:\Steam\steam.exe -login fenriros3 password -applaunch 440 -textmode"]]

做完后,我将使用-exit关闭lua窗口。

我意识到我的代码并不是非常高效的,但这是以后担心的问题。现在我只需要让它工作。

非常感谢任何帮助,如果我错过了一些显而易见的东西,我表示歉意,我对Lua仍然很陌生。

点赞
用户438753
用户438753

第一个显而易见的方法。[[ ]]用于定界一个字符串,所以你只需要为这个字符串创建一个变量,并根据需要替换其内容。

function Steam(n, opt1, opt2, opt3)
-- 设置带有参数占位符的执行字符串
local strExecute = [["start C:\Program" "Files\Sandboxie\Start.exe /box:Steam{n} D:\Steam\steam.exe -login fenriros{n} password -{opt1} -{opt2} -{opt3}"]]

-- 使用 gsub 替换参数
-- 也可以用字符串连接,但我更喜欢这种方法。
strExecute = strExecute:gsub('{n}',n)
strExecute = strExecute:gsub('{opt1}',opt1:gsub('%%','%%%%'))
strExecute = strExecute:gsub('{opt2}',opt2:gsub('%%','%%%%'))
strExecute = strExecute:gsub('{opt3}',opt3:gsub('%%','%%%%'))
os.execute(strExecute)
end

Steam(1,'r1','r2','r3')
2013-03-26 08:10:55