PHP syntax check of all files in a directory tree

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
Bertil
Level 1
Level 1
Posts: 8
Joined: Thu Dec 07, 2017 12:35 pm
Location: Kolsva, Sweden
Contact:

PHP syntax check of all files in a directory tree

Post by Bertil »

This bash script is useful if you for example are developing web software using PHP.
After editing several PHP files in a directory structure I use this script to check for syntax errors. I have developed and translated several language packages for osCommerce webshop software using this script. You can probably use the structure of this script to traverse down a directory tree and do other things with found files. Please tell me if I presented the script in the wrong way

Code: Select all


#!/bin/bash
# This shell script syntax checks all PHP files from the specified directory
# and downwards and print the file name and the error found, and a summary.
# Remember to save this file with the name php_check and run the command
# chmod 755 php_check to make the file executable. 
# Written by Bertil Palmqvist 2016.
if [ "$#" -eq 0 ]
then
echo "Usage: php_check directoryname" 
exit 0
fi
i=0;
j=0;
k=0;
for x in `find ./$1 -type f`
do
filetype=`file -b $x`
ft=`echo $filetype | cut -c1-3`
if [ "$ft" == "PHP" ]
 then
 bercheck=`php -l $x`
 bercheck_begin=`echo $bercheck | cut -c1-2`
    if [ "$bercheck_begin" == "Er" ] # No if no syntax error, Er if error
     then
         k=`expr $k + 1`
     else
         i=`expr $i + 1`
    fi
 else
j=`expr $j + 1`
fi
done
echo $i "PHP files with No syntax errors"
echo $k "PHP files with Parse error/Syntax error"
echo $j "other files than PHP files not checked"


Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Bertil
Level 1
Level 1
Posts: 8
Joined: Thu Dec 07, 2017 12:35 pm
Location: Kolsva, Sweden
Contact:

Re: PHP syntax check of all files in a directory tree

Post by Bertil »

Hi, I run shellcheck on my script, and found that I needed to change from for loop to while loop, and some old syntax.

Code: Select all

#!/bin/bash
# This shell script syntax checks all PHP files from the specified directory
# and downwards and print the file name and the error found, and a summary.
# Remember to save this file with the name php_check and run the command
# chmod 755 php_check to make the file executable. 
# Written by Bertil Palmqvist 2016.
# after running ShellCheck the following suggestions were stated:
# ^-- SC2006: Use $(..) instead of legacy `..`.
# ^-- SC2086: Double quote to prevent globbing and word splitting.
#  ^-- SC2003: expr is antiquated. Consider rewriting this using $((..)), ${} or [[ ]].
#  ^-- SC2044: For loops over find output are fragile. Use find -exec or a while read loop.
# so I changed to while loop and updated other suggestions
if [ "$#" -eq 0 ]
then
echo "Usage: php_check directoryname" 
exit 0
fi
i=0;
j=0;
k=0;
find ./"$1" -type f > tmp
while IFS= read -r x
do
filetype=$(file -b "$x")
ft=$(echo "$filetype" | cut -c1-3)
if [ "$ft" == "PHP" ]
 then
 bercheck=$(php -l "$x")
 bercheck_begin=$(echo "$bercheck" | cut -c1-2)
    if [ "$bercheck_begin" == "Er" ] # No if no syntax error, Er if error
     then
         let k++
     else
         let i++
    fi
 else
let j++
fi
done < tmp
echo "$i" "PHP files with No syntax errors"
echo "$k" "PHP files with Parse error/Syntax error"
echo "$j" "other files than PHP files not checked"

Locked

Return to “Scripts & Bash”