【发布时间】:2011-02-14 08:23:48
【问题描述】:
我试图让 bash 处理来自标准输入的数据,这些数据通过管道输入,但没有运气。我的意思是没有以下工作:
echo "hello world" | test=($(< /dev/stdin)); echo test=$test
test=
echo "hello world" | read test; echo test=$test
test=
echo "hello world" | test=`cat`; echo test=$test
test=
我希望输出为test=hello world。我尝试在 "$test" 周围加上 "" 引号,但这也不起作用。
【问题讨论】:
-
你的例子.. echo "hello world" |阅读测试; echo test=$test 对我来说效果很好.. 结果: test=hello world ;这是在什么环境下运行的?我正在使用 bash 4.2..
-
您想一次读取多行吗?您的示例只显示了一行,但问题描述不清楚。
-
@alex.pilon,我正在运行 Bash 版本 4.2.25,他的示例也不适用于我。可能是 Bash 运行时选项或环境变量的问题?我的示例也不适用于 Sh,所以 Bash 可以尝试与 Sh 兼容吗?
-
@Hibou57 - 我在 bash 4.3.25 中再次尝试过,但它不再有效。我对此的记忆很模糊,我不确定我做了什么才能让它发挥作用。
-
@Hibou57 @alex.pilon 管道中的最后一个 cmd 应该影响 bash4>=4.2 中的变量
shopt -s lastpipe-- tldp.org/LDP/abs/html/bashver4.html#LASTPIPEOPT