I am trying to lint some SQL code using regex and have managed to patch together some code. However the grep returns a new line character when I run it in a bash file but returns nothing when I run it from the command line. I suspect there is something wrong in my bash file.
LINT='pcregrep -Mi'
PATTERN="(^\s*SELECT(.*)(\n|\r\n|\r))((^(\s*--(.*))|^(\s*(.*),))(\n|\r\n|\r))*(^(\s*--(.*))(\n|\r\n|\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\033[32mPassed: ${file}\033[0m"
else
echo -e "\t\033[31mFailed: ${file}\033[0m"
echo $result | tr ' \n' '#$'
echo "$($LINT $PATTERN ${file})"
pass=false
fi
done
Here some code that I test on,
SELECT
val1,
val2,
val3
FROM