如何在libuv luv中使用shell pipeline而不使用sh-c

假设我想运行命令dmesg |rg-i hda。我如何使用process.spawn或任何其他libuv luv中的异步函数运行它并捕获输出,而不生成带有sh-c"dmesg |rg-i hda"的shell?

点赞
用户6632736
用户6632736

尝试使用 io.popen()

local handle = io.popen 'dmesg | rg -i hda'
local result = handle:read '*all'
handle:close()

print (result)

P.S. 我不明白为什么你使用 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 ()
2020-10-31 10:17:24