sab*_*son 2 grep find hp-ux shell-script
我有一个要在文件列表中查找单词列表。因此,我将单词列表放在一个文件中,并使用for循环尝试读取文件中的每个单词并使用 find 命令grep在找到的文件列表中查找该单词。
我正在使用提到的 Linux 风格
bash-3.1$ uname
HP-UX
Run Code Online (Sandbox Code Playgroud)
下面提到的是我的shell脚本。
#!/bin/sh
PATH="/NO/20171013"
STRING=`/usr/bin/cat /home/test/STAT44_test.txt`
for LINE in ${STRING}
do
echo "find ${PATH} -type f -name \"*.txt\" -exec grep -w "${LINE}" {} \; 2>/dev/null | /usr/bin/wc -l"
LINES=`find ${PATH} -type f -name "*.txt" -exec grep -w "${LINE}" {} \; 2>/dev/null | /usr/bin/wc -l`
echo "LINES count is ${LINES}"
if [ ${LINES} -eq 0 ]
then
echo "In not found"
# echo "${LINE} is not found in ${PATH}" >> /home/test/STAT44_not_found.out
else
echo "In Found"
# echo "${LINE} is found in ${PATH}" >> /home/test/STAT44_found.out
fi
done
Run Code Online (Sandbox Code Playgroud)
find 命令find ${PATH} -type f -name "*.txt" -exec grep -w '${LINE}' {} \; 2>/dev/null在命令提示符下工作得很好,而如果在上面提到的 shell 脚本中使用,它不会提供任何输出。
的输出echo $?为 0,但 find 命令不产生任何输出。
脚本的输出是
find /NO/20171013 -type f -name "*.txt" -exec grep -w 655044810645 {} \; 2>/dev/null | /usr/bin/wc -l
LINES count is 0
In not found
find /NO/20171013 -type f -name "*.txt" -exec grep -w 734729751028 {} \; 2>/dev/null | /usr/bin/wc -l
LINES count is 0
In not found
Run Code Online (Sandbox Code Playgroud)
实际上*.txt下面的/NO/20171013文件有模式655044810645和734729751028. 因此Word count不应为 0,它必须进入else一部分。
可能缺少什么?