我想知道为什么这个脚本即使有明确的退出命令仍然继续运行。
我有两个文件:
file1.txt
包含以下内容:
aaaaaa bbbbbb cccccc dddddd eeeeee ffffff gggggg
file2.txt
包含以下内容:
111111 aaaaaa 222222 333333 ffffff 444444
这个脚本(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
即使应该在打印第一个 !!!!!!!!!!
后退出,但我仍然得到以下输出:
aaaaaa !!!!!!!!!! bbbbbb cccccc dddddd eeeeee ffffff !!!!!!!!!! gggggg
exit
不应该完全结束脚本的执行吗?
while
。管道将为while
启动另一个子进程(shell),因此while
中的exit
退出该 shell,你回到了原来的位置。 - lurker