Bash脚本不会循环退出 [英] Bash script does not exit in loop
本文介绍了Bash脚本不会循环退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在HP/UX的以下脚本中运行并获得了输出:
I have run at below script in HP/UX and get that output:
Exiting #1
Exiting #2
但是我希望输出:
Exiting #1
脚本文件:
data="aaa; bbb; ccc"
echo $data | while IFS=';' read -ra array; do
echo "Exiting #1"
exit -1
done
echo "Exiting #2"
exit 0
我该如何解决这个问题?谢谢.
How can I solve this problem? Thanks.
推荐答案
由于管道的原因,循环中的命令在子shell中运行.当您调用exit时,仅终止子Shell,而不终止父进程.
Because of the pipe, the command in the loop run in a subshell. When you call exit, only the subshell is terminated, and not the parent process.
您可以通过使用 shopt -s lastpipe
,使用进程替换来解决此问题
You can overcome this by using shopt -s lastpipe
, by using process substitution
while IFS=';' read -ra array; do
echo "Exiting #1"
exit -1
done < <(echo "$data")
或使用此处字符串
while IFS=';' read -ra array; do
echo "Exiting #1"
exit -1
done <<< "$data"
这篇关于Bash脚本不会循环退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文