问题描述
Bash 允许使用:cat <(echo "$FILECONTENT")
Bash 也允许使用:while read i;做回声 $i;完成 </etc/passwd
Bash also allow to use: while read i; do echo $i; done </etc/passwd
结合前两个可以使用: echo $FILECONTENT |读我的时候;做回声 $i;完成
to combine previous two this can be used: echo $FILECONTENT | while read i; do echo $i; done
最后一个的问题是它创建了子shell并且在while循环结束后变量i
不能再被访问.
The problem with last one is that it creates sub-shell and after the while loop ends variable i
cannot be accessed any more.
我的问题是:
如何实现这样的事情:while read i;做回声 $i;done <(echo "$FILECONTENT")
或者换句话说:我如何确保 i
在循环中存活?
How to achieve something like this: while read i; do echo $i; done <(echo "$FILECONTENT")
or in other words: How can I be sure that i
survives while loop?
请注意,我知道将 while 语句包含在 {}
中,但这并不能解决问题(想象一下您想在函数中使用 while 循环并返回 i
> 变量)
Please note that I am aware of enclosing while statement into {}
but this does not solves the problem (imagine that you want use the while loop in function and return i
variable)
推荐答案
进程替换是:
while read i; do echo $i; done < <(echo "$FILECONTENT")
在循环中分配的 i
的最后一个值然后在循环终止时可用.另一种选择是:
The last value of i
assigned in the loop is then available when the loop terminates.An alternative is:
echo $FILECONTENT |
{
while read i; do echo $i; done
...do other things using $i here...
}
大括号是 I/O 分组操作,本身不会创建子 shell.在这种情况下,它们是管道的一部分,因此作为子 shell 运行,但这是因为 |
,而不是 { ... }
.你在问题中提到了这一点.AFAIK,您可以在函数内部从这些内部返回.
The braces are an I/O grouping operation and do not themselves create a subshell. In this context, they are part of a pipeline and are therefore run as a subshell, but it is because of the |
, not the { ... }
. You mention this in the question. AFAIK, you can do a return from within these inside a function.
Bash 还提供了 shopt
内置,其众多选项之一是:
Bash also provides the shopt
builtin and one of its many options is:
最后一个管道
如果设置,并且作业控制未处于活动状态,shell 将运行当前 shell 环境中未在后台执行的管道的最后一个命令.
If set, and job control is not active, the shell runs the last command of a pipeline not executed in the background in the current shell environment.
因此,在脚本中使用类似这样的东西会使修改后的sum
在循环后可用:
Thus, using something like this in a script makes the modfied sum
available after the loop:
FILECONTENT="12 Name
13 Number
14 Information"
shopt -s lastpipe # Comment this out to see the alternative behaviour
sum=0
echo "$FILECONTENT" |
while read number name; do ((sum+=$number)); done
echo $sum
在命令行中执行此操作通常会违反作业控制未激活"(即,在命令行中,作业控制处于活动状态).不使用脚本进行测试失败.
Doing this at the command line usually runs foul of 'job control is not active' (that is, at the command line, job control is active). Testing this without using a script failed.
此外,正如 Gareth Rees 在他的 answer,您有时可以使用 此处字符串:
Also, as noted by Gareth Rees in his answer, you can sometimes use a here string:
while read i; do echo $i; done <<< "$FILECONTENT"
这不需要shopt
;您可以使用它来保存进程.
This doesn't require shopt
; you may be able to save a process using it.
这篇关于如何在循环结束后将输入通过管道传输到 Bash 循环并保留变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!