Shell 脚本:检查不区分大小写的字符串

Shell scripting: Check for a string case insensitive

我需要检查所有 DROP TABLE 语句是否存在子句。

如果 IF EXISTS 子句不存在,脚本应该打印它。下面是我的代码。它工作正常,但是我必须检查所有情况。以下场景仅适用于大写字母。

for f in $FILES
do
result=`grep "DROP \+TABLE" "$f" | grep -v "IF \+EXISTS"` # -v inverts the match
if [ ! -z $result ]
then
echo 'IF EXISTS clause not found ' $f ':' $result 
fi
done

您可以使用 grep-i 选项:

grep -i "DROP \+TABLE" "$f" | grep -iv "IF \+EXISTS"

来自grep's man page

-i, --ignore-case

Ignore case distinctions in both the PATTERN and the input files. (-i is specified by POSIX.)