如何在libuv luv进程中使用shell管道而不使用sh -c

问题描述

假设我要运行命令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 ()