#!/bin/bash if [ ! "$(pwd | grep scripts$)" ] ; then echo "Working directory is wrong" exit 1; fi BASE_PATH=`pwd | xargs dirname` folder=$1 if [ "$folder" == "" ] ; then folder=$BASE_PATH fi echo 'Cleaninig BOM markers from ' $folder #expand the char codes in the regex to their corresponding characters regex1=$'\xEF\xBB\xBF' cmd1="grep -rl $regex1 $folder" #select only php, ini, html, and js files regex2='\.\(php\|ini\|html\|js\)$' cmd2="grep $regex2" files=$($cmd1 | $cmd2) if [ "$files" == "" ] ; then echo 'All files already cleaned up'; exit; fi echo "Cleaning up:" echo "$files" | \ while read f; do echo ' ' $f; # back up file; only if not yet backed up backup="$f.backup" if [ ! -e "$backup" ]; then cp "$f" "$backup" fi # the copy operation above could have failed anyway (permissions) # don't proceed if there is no backup file if [ -e "$backup" ]; then # delete the original file rm -f "$f" # output the backup file without the BOM # and write it to the original file location sed "s/\xEF\xBB\xBF//" "$backup" > "$f" rm -f "$backup" fi done