Skip to content

Instantly share code, notes, and snippets.

@fabarea
Created February 11, 2014 15:05
Show Gist options
  • Save fabarea/8936524 to your computer and use it in GitHub Desktop.
Save fabarea/8936524 to your computer and use it in GitHub Desktop.
Recursively remove the php closing tag `?>` and any following blank lines from the end of any PHP file. Source directories can be configured.
#!/bin/bash
###############################
# Recursively remove the closing php tag `?>` and any following blank lines from the
# end of any PHP file. Source directories can be configured.
#
# Author: Fabien Udriot <fabien.udriot@ecodev.ch>
# Date: 2014-02-11
###############################
# Configurable source directories to be scanned.
# example: folders=(foo bar)
folders=(Classes)
for folder in "${folders[@]}"
do
files=$(find $folder -type f -name "*.php");
for match in $files;
do
file=`echo $match | awk -F ':' '{print $1}'`;
lineCount=`wc -l $file | awk -F " " '{print $1}'`;
echo "Removing lines through ${lineCount} from file $file...";
sed 's/?>//g' $file > $file.bak
sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' $file.bak > $file
rm $file.bak;
done;
done
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment