有没有办法让鱼函数从管道读取可选输入? [英] Is there any way for a fish function to read an optional input from pipe?

查看:55
本文介绍了有没有办法让鱼函数从管道读取可选输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从函数内部读取已通过管道传输到函数的数据,但前提是有数据?我用这样的猫试过:

Is there any way to read data that has been piped to the function, from inside the function, but only if there is any? I tried it with cat like this:

function myPipe
    set -l pipe_input (cat -)
    echo $pipe_input # is never reached
end
echo test | myPipe

然而,这会导致函数停止并等待管道输入,即使实际上有数据通过管道传输到函数.

However, this results in the function stopping and waiting for pipe input, even if there actually was data piped to the function.

我希望fish执行函数的其余部分,即使没有管道输入,以便我可以处理管道数据.

I would like fish to execute the rest of the function, even if there is no pipe input, so that I can handle the piped data.

推荐答案

您可以测试 stdin 是否连接到管道:

You can test if stdin is connected to a pipe:

if isatty stdin
    set -l pipe_input ""     # not a pipe or redirection
else
    set -l pipe_input (cat -)
end

参考:https://fishshell.com/docs/current/commands.html#isatty

这篇关于有没有办法让鱼函数从管道读取可选输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆