问题描述
真的希望有人能帮助我理解这种行为以及如何解决它。
> 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⏎
解决方法
您误解了 $argv
函数局部变量是如何初始化的。它没有设置为 stdin 的内容。它被设置为函数的位置参数。例如,hello (world)
将产生您期望的输出。如果您希望 edit
函数从 stdin 捕获其数据,则需要显式读取 stdin。
正如@Kurtis-Rader 的回答所提到的,要访问 fish 函数中的管道,您可以使用 read
命令(请参阅 man read
)。这类似于 bash、sh、zsh 等。
鱼的示例(在评论中讨论了编辑以使管道输入可选):
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
要从管道读取多行输入,请将 read
包装在 while
语句中。