问题描述
假设我要运行命令dmesg | rg -i hda
。我该如何使用process.spawn
或libuv luv中的任何其他异步函数来运行它,并在不使用sh -c "dmesg | rg -i hda"
生成shell的情况下捕获输出?
解决方法
尝试io.popen()
:
local handle = io.popen 'dmesg | rg -i hda'
local result = handle:read '*all'
handle:close()
print (result)
PS 。我不明白您为什么使用rg
而不是grep
。另外,过滤可以由Lua自己完成:
local handle = io.popen 'dmesg'
for line in handle:lines () do
if line:lower ():match 'hda' then
print (line)
end
end
handle:close ()