gpt4 book ai didi

Bash 退出不退出

转载 作者:行者123 更新时间:2023-11-29 08:54:05 28 4
gpt4 key购买 nike

我想知道为什么即使有明确的退出命令,这个脚本也会继续运行。

我有两个文件:

file1.txt 包含以下内容:

aaaaaabbbbbbccccccddddddeeeeeeffffffgggggg

file2.txt with the following content:

111111aaaaaa222222333333ffffff444444

The script (test.sh) is this, two nested loops checking if any line of the first file contains any line of the second file. If it finds a match, it aborts.

#!/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!!!!!!!!!!bbbbbbccccccddddddeeeeeeffffff!!!!!!!!!!gggggg

exit 不是应该完全结束脚本的执行吗?

最佳答案

原因是管道创建了子进程。改用输入重定向,它应该可以工作

#!/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

在一般情况下,输入来自另一个程序而不是文件,您可以使用 process substitution

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)

关于Bash 退出不退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18359777/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com