我正在尝试做类似的事情
var=0
grep "foo" bar | while read line; do
var=1
done
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不起作用,因为管道导致while在子shell中运行.有一个更好的方法吗?如果有另一种解决方案,我不需要使用"读取".
我看过类似的Bash变量范围,但我无法从中获得任何有用的东西.
Kal*_*son 21
如果你真的在做一些简单的事情,你甚至不需要while read循环.以下将有效:
VAR=0
grep "foo" bar && VAR=1
# ...
Run Code Online (Sandbox Code Playgroud)
如果你确实需要循环,因为循环中正在发生其他事情,你可以从<( commands )进程替换重定向:
VAR=0
while read line ; do
VAR=1
# do other stuff
done < <(grep "foo" bar)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9792 次 |
| 最近记录: |