我试图使用正则表达式检测一些SQL代码,并设法将一些代码修补在一起。然而,当我在bash文件中运行grep时,它返回一个新的行字符,但当我从命令行运行它时,它什么也不返回。我怀疑我的bash文件有问题。
LINT='pcregrep -Mi'
PATTERN="(^s*SELECT(.*)(n|rn|r))((^(s*--(.*))|^(s*(.*),))(n|rn|r))*(^(s*--(.*))(n|rn|r))*(^s*FROM(.*))"
files=$(git diff --name-only --diff-filter=ACM | grep ".sql$")
if [ "$files" = "" ]; then
exit 0
fi
pass=true
echo -e "nLinting SQL:n"
for file in ${files}; do
result=$($LINT $PATTERN ${file} | grep "No syntax errors detected in ${file}")
if [ "$result" != "" ]; then
echo -e "t 33[32mPassed: ${file} 33[0m"
else
echo -e "t 33[31mFailed: ${file} 33[0m"
echo $result | tr ' n' '#$'
echo "$($LINT $PATTERN ${file})"
pass=false
fi
done
这里是我测试的代码,
SELECT
val1,
val2,
val3
FROM
语句
"$result" != ""
应该
"$result" = ""
如果通过则为空。