Tul*_*ova 8 bash exit abort nested-loops
我想知道为什么即使使用显式退出命令,此脚本仍继续运行.
我有两个文件:
file1.txt
具有以下内容:
aaaaaa bbbbbb cccccc dddddd eeeeee ffffff gggggg
file2.txt
具有以下内容:
111111 aaaaaa 222222 333333 ffffff 444444
script(test.sh
)就是这样,两个嵌套循环检查第一个文件的任何行是否包含第二个文件的任何行.如果找到匹配项,则会中止.
#!/bin/bash
path=`dirname $0`
cat $path/file1.txt | while read line
do
echo $line
cat $RUTA/file2.txt | while read another
do
if [ ! -z "`echo $line | grep -i $another`" ]; then
echo "!!!!!!!!!!"
exit 0
fi
done
done
Run Code Online (Sandbox Code Playgroud)
我得到以下输出,即使在打印第一个后它应该退出!!!!!!!!!!
:
aaaaaa !!!!!!!!!! bbbbbb cccccc dddddd eeeeee ffffff !!!!!!!!!! gggggg
是不是exit
应该完全结束脚本的执行?
use*_*001 14
原因是管道创建子流程.使用输入重定向代替它应该工作
#!/bin/bash
while read -r line
do
echo "$line"
while read -r another
do
if grep -i "$another" <<< "$line" ;then
echo "!!!!!!!!!!"
exit 0
fi
done < file2.txt
done < file1.txt
Run Code Online (Sandbox Code Playgroud)
在一般情况下,输入来自另一个程序而不是来自文件,您可以使用进程替换
while read -r line
do
echo "$line"
while read -r another
do
if grep -i "$another" <<< "$line" ;then
echo "!!!!!!!!!!"
exit 0
fi
done < <(command2)
done < <(command1)
Run Code Online (Sandbox Code Playgroud)
while 循环在各自的 shell 中运行。退出一个 shell 不会退出包含的 shell。$? 可能是你的朋友:
...
echo "!!!!!!!!!!"
exit 1
fi
done
[ $? == 1 ] && exit 0;
done
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11199 次 |
最近记录: |