[英]cat filename | grep -i doesn't work on .sh script
我需要讀取一個日志文件,並檢查該文件中是否有某個字符串。
我實際上正在嘗試在bash上運行以下命令,它似乎可以工作:
cat $f | grep -m1 'Input file for table '"$current_table"' NOT FOUND'
哪里:
$f
=日志文件; $current_table
=變量以串聯在我要查找的字符串中。 關鍵是我必須將此代碼放在.sh
腳本中,如下所示:
if [ "$(cat $f | grep -m1 'Input file for table '"$current_table"' NOT FOUND')" != "" ]; then
沒用
然后,我嘗試在bash中運行以下代碼,並且再次運行,語法完全相同:
echo "$(cat $f | grep -m1 'Input file for table '"$current_table"' NOT FOUND')"
誰能解釋我錯了嗎? 有什么建議嗎?
嘗試這個:
# redirected to /dev/null to avoid output
if grep -m1 "Input file for table $current_table NOT FOUND" "$f" >/dev/null
then
echo 'found'
else
echo 'not found'
fi
您不需要帶有grep
(和tail
, sed
等)過濾程序的cat
。
如果要test
命令if
成功(在本例中為grep
), if
不需要此處的test
命令(也稱為[
)。
假設設置了f
和current_table
,我還簡化了您的引用。 確保引用文件名( "$f"
),以防它們包含空格。
我的第一個猜測是腳本中未定義current_table
變量。
在腳本中,應確保同時定義了f
和current_table
變量並分配了值。
嘗試一下:
if [ -z "${current_table}" -o -z "${f}" ] ; then
printf "one or both variables has no value\n"
printf "current_table=%s\nf=%s\n" "${current_table}" "${f}"
exit 1
fi
grep -q "Input file for table ${current_table} NOT FOUND" "${f}"
if [ $? -ne 0 ] ; then
printf "the line below was not found in the %s file.\n" "${f}"
printf "Input file for table %s NOT FOUND\n" "${current_table}"
fi
不需要cat
命令,因為grep
可以直接搜索文件。 優先使用printf
提出問題時通常最好提供一個最小的工作示例,這有助於人們重現您的問題。
日志/模式中的"
可能存在問題。
這是您要嘗試執行的工作示例。 我在grep
添加了-n
,因此您可以看到匹配的行:
/t/tmp.mCbnzfsCnb> cat logfile
line 1
Input file for table Users NOT FOUND
line 3
Input file for table "Users" NOT FOUND
line 5
。
/t/tmp.mCbnzfsCnb> cat grep-example.sh
#!/bin/bash
set -exuo pipefail
f=logfile
current_table=Users
if [ "$(cat $f | grep -n -m1 'Input file for table '"$current_table"' NOT FOUND')" != "" ]; then
echo "OK - no quotes"
fi
if [ "$(cat $f | grep -n -m1 'Input file for table "'$current_table'" NOT FOUND')" != "" ]; then
echo "OK - quotes in the log"
fi
# Alternative syntax (no cat, no test)
if grep -n -m1 'Input file for table '"$current_table"' NOT FOUND' "$f"; then
echo "OK - alternative no quotes"
fi
if grep -n -m1 'Input file for table "'$current_table'" NOT FOUND' "$f"; then
echo "OK - alternative no quotes"
fi
。
/t/tmp.mCbnzfsCnb> ./grep-example.sh
+ f=logfile
+ current_table=Users
++ cat logfile
++ grep -n -m1 'Input file for table Users NOT FOUND'
+ '[' '2:Input file for table Users NOT FOUND' '!=' '' ']'
+ echo 'OK - no quotes'
OK - no quotes
++ cat logfile
++ grep -n -m1 'Input file for table "Users" NOT FOUND'
+ '[' '4:Input file for table "Users" NOT FOUND' '!=' '' ']'
+ echo 'OK - quotes in the log'
OK - quotes in the log
+ grep -n -m1 'Input file for table Users NOT FOUND' logfile
2:Input file for table Users NOT FOUND
+ echo 'OK - alternative no quotes'
OK - alternative no quotes
+ grep -n -m1 'Input file for table "Users" NOT FOUND' logfile
4:Input file for table "Users" NOT FOUND
+ echo 'OK - alternative no quotes'
OK - alternative no quotes
請注意,這兩個選項之間的區別非常微妙: "'
vs '"
另請注意,在進行故障排除時可以使用set -x
逐步查看正在執行的操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.