[英]In fish shell, why can't one function's output pipe to another?
Really hoping someone can help me to understand this behavior and how to work around it.真的希望有人可以帮助我理解这种行为以及如何解决它。
> functions hello
# Defined in ...
function hello
echo -n $argv[1] "hello"
end
> functions world
# Defined in ...
function world
echo -n "world"
end
> hello
hello⏎
> world
world⏎
> world | hello
hello⏎
You have misunderstood how the $argv
function local variable is initialized.您误解了
$argv
function 局部变量是如何初始化的。 It is not set to the contents of stdin.它没有设置为标准输入的内容。 It is set to the positional arguments of the function.
它设置为 function 的位置 arguments。 For example,
hello (world)
will produce the output you expect.例如,
hello (world)
将生成您期望的 output。 If you want your edit
function to capture its data from stdin you need to explicitly read stdin.如果您希望
edit
function 从标准输入捕获其数据,您需要显式读取标准输入。
As @Kurtis-Rader's answer mentioned, to access a pipe in a fish function, you use the read
command (see man read
).正如@Kurtis-Rader 的回答所提到的,要访问鱼 function 中的 pipe,请使用
read
命令(请参阅man read
)。 This is similar to bash, sh, zsh, etc.这类似于bash、sh、zsh等。
Example for fish (with edits discussed in the comments to make the piped input optional ):鱼的示例(在评论中讨论了使管道输入可选的编辑):
function greeting
echo "Good Morning"
end
function world
if isatty stdin
set greetstring "Hello"
else
read greetstring
end
echo (string trim $greetstring)", World"
end
> greeting | world
Good Morning, World
> world
Hello, World
To read multiline input from a pipe, you wrap the read
in a while
statement.要从 pipe 读取多行输入,请将
read
包装在while
语句中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.