Skip to content

Instantly share code, notes, and snippets.

@jmhobbs
Created January 3, 2012 17:55
Show Gist options
  • Save jmhobbs/1556081 to your computer and use it in GitHub Desktop.
Save jmhobbs/1556081 to your computer and use it in GitHub Desktop.
This is a little bash script I use to quickly check the syntax on a directory of PHP files.
jmhobbs@Cordelia:/data/working/example$ (193m|master|?AM)$ phlint
= Checking 176 files with 4 processes...
(1) = 50
(2) = 100
(3) = 150
(1) Done
(0) Done
(3) Done
(2) Done
jmhobbs@Cordelia:/data/working/example$ (193m|master|?AM)$
#!/bin/bash
# Multi-processor version for faster checks!
checkfiles () {
I=$2
for file in $1; do
php -l $file > /dev/null
I=$(($I+1))
OFFSET=$(($I%50))
if [ "$OFFSET" == "0" ]; then
echo "($3) = $I"
fi
done
echo "($3) Done"
}
TOTAL=$(find . -iname '*.php' | wc -l)
PROCS=$(cat /proc/cpuinfo | grep 'cpu cores' | head -n 1 | awk '{print $4}')
echo "= Checking $TOTAL files with $PROCS processes..."
# This is a bit brute force, but it works
BLOCKSIZE=$(($TOTAL / $PROCS))
ASSIGNED=0
for PROC in $(seq 0 $(($PROCS - 1 ))); do
STARTINGPOINT=$ASSIGNED
if [ "$(($PROC + 1))" == "$PROCS" ]; then
FILES=$(find . -iname '*.php' | head -n -$ASSIGNED)
THISBLOCK=$(find . -iname '*.php' | head -n -$ASSIGNED | wc -l)
ASSIGNED=$(($ASSIGNED + $THISBLOCK))
else
OFFSET=$((($PROC + 1) * $BLOCKSIZE))
FILES=$(find . -iname '*.php' | head -n $OFFSET | tail -n $BLOCKSIZE)
THISBLOCK=$(find . -iname '*.php' | head -n $OFFSET | tail -n $BLOCKSIZE | wc -l)
ASSIGNED=$(($ASSIGNED + $THISBLOCK))
fi;
( checkfiles "$FILES" $STARTINGPOINT $PROC ) &
done
# Wait on the sub-shells
while true; do
ALIVE=$(ps -ef | grep $$ | grep -v grep | grep -v 'ps -ef' | wc -l)
# This is 2 instead of 1 because the $() counts as a sub-shell
if [ "2" == "$ALIVE" ]; then
break;
else
sleep 1
fi
done
echo "= Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment