我试图从一个目录中的每行文件中获取一些信息。我使用while循环来grep文件的每一行。单独的grep可以完美地工作。我用echo测试了我的while循环,它工作得很好,但是当我在里面使用grep时,它没有给我任何输出。
while IFS= read -r LINE; do
grep --include=\requests-definition.const.ts -rnwH $DIR -e "$LINE";
echo $LINE;
done < key_list
我的key_list是一个文本文件,每行都有一个键,当我单独使用grep时,它可以工作,但在while循环中可能无法工作。
谢谢!
确保你的循环不是在第一行没有匹配的情况下退出。
从grep人
退出状态
Normally the exit status is 0 if a line is selected, 1 if no lines were selected, and 2 if
an error occurred. However, if the -q or --quiet or --silent is used and a line is
selected, the exit status is 0 even if an error occurred.
要想退出,你有几种方法。
while IFS= read -r LINE; do
set +x # don't exit if exit code is different from 0
grep --include=\requests-definition.const.ts -rnwH $DIR -e "$LINE";
set -e # exit if exit code is different from 0
echo $LINE;
done < key_list
通过使用dos2unix命令从key_list中删除特殊字符,问题得到解决。