Function 在鱼壳函数中,如何将stdin管道传输到变量?
以下是我得到的:Function 在鱼壳函数中,如何将stdin管道传输到变量?,function,fish,Function,Fish,以下是我得到的: function stdin2var set a (cat -) echo $a end 第一个例子: $ echo 'some text' | stdin2var # should output "some text" 第二个例子: $ echo some text\nsome more text | stdin2var # should output: "some text some more text" 有什么建议吗?在鱼壳(和其他)中,您需要阅读:
function stdin2var
set a (cat -)
echo $a
end
第一个例子:
$ echo 'some text' | stdin2var
# should output "some text"
第二个例子:
$ echo some text\nsome more text | stdin2var
# should output: "some text
some more text"
有什么建议吗?在鱼壳(和其他)中,您需要阅读:
echo 'some text' | read varname
按照@lubic_fish的答案,使用while循环消耗所有输入:
function stdin2var
set -l a
while read line
set a $a $line
end
# $a is a *list*, so use printf to output it exactly.
echo (count $a)
printf "%s\n" $a
end
所以你得到了
$ echo foo bar | stdin2var
1
foo bar
$ seq 10 | stdin2var
10
1
2
3
4
5
6
7
8
9
10
如果管道包含换行分隔的内容,则仅保留第一行。我遗漏了什么?@荒谬的鱼,我不明白为什么set a(cat-
不使用函数的stdin。你能详细说明一下吗?这是因为fish不会将块重定向传播到命令替换中。也许应该。使用read-z
进行以空结尾的多行输入(基本上是通过管道输入的),我想知道为什么人们对我的问题投了反对票。如果有需要改进的地方,我很乐意对其进行编辑。令人恼火的是,如果stdin中没有任何内容,while read命令将等待用户输入,这不是您的答案的错误,而是Fish的错误。