尝试使用Lua的io.popen(..,'w')
构造后台常驻的批处理(Batch)宿主,减小每次命令都调用io.popen-close
的开销。
可用,但Lua下存在结束流信号EOF的问题,且需求环境下,有更好的方式,故放弃,仅存档。
构造的Bat-"loop_execute_input_command_until_exit.bat":
- 输入的第一行为后续命令的输出重定向目标。
- 后续行为命令,可动态接受命令输入,执行。
- 使用
exit
命令退出。
在CMD下测试:
..>loop_execute_input_command_until_exit.bat
>(re)start, waiting input:
&1
echo hello world
^Z
>output to: &1.
>command to execute: "echo hello world".
hello world
>input finish.
>(re)start, waiting input:
exit
^Z
可用文件形式loop_execute_input_command_until_exit.bat < test_input.txt
。
另见:test.lua \ test bat exectue command from input file。
构造的Lua-"CommandPipe.lua":
-
commandPipe=CommandPipe(output)
output:-
nil
:stdout
, -
false
: wont output (nul
), -
filename
: 初始重定向目标,命令中可以覆盖。
-
-
commandPipe:write'command'
(Call)、commandPipe..'command'
(Concat)、commandPipe['command']
(Index)。 -
commandPipe()
close。
test.lua。含:命令行、Lua CommandPipe
、耗时测试。output:
test bat exectue command from input file:
start
>(re)start, waiting input:
>output to: test_output.txt.
>command to execute: "echo hello world, wait".
>command to execute: "ping 192.0.2.2 -n 1 -w 1000 > nul".
>command to execute: "echo goodbye".
done. result OK
test "CommandPipe":
try to un-comment `REM` and `::` in .bat to see process.
finish input
>(re)start, waiting input:
>output to: test_output.txt.
>command to execute: "echo hello".
>command to execute: "echo world, waiting".
>command to execute: "ping 192.0.2.2 -n 1 -w 5000 > nul".
>command to execute: "echo goodbye".
finish process
result OK
test time cost:
when repeat 5 times.
basic popen cost: 0.13012146949768
traditional method: 0.15064239501953
>(re)start, waiting input:
>output to: nul.
>command to execute: "echo 1 >>test_output.txt".
>command to execute: "echo 2 >>test_output.txt".
>command to execute: "echo 3 >>test_output.txt".
>command to execute: "echo 4 >>test_output.txt".
>command to execute: "echo 5 >>test_output.txt".
command pipe method: 0.14091825485229
test done
all finish