Project

General

Profile

cleanBOM.sh

LeGars, 04/20/2010 09:59 AM

 
1
#!/bin/bash
2
#The cleanBOM.sh bash script
3
#written by erik@sankuru.biz Cette adresse email est protégée contre les robots des spammeurs, vous devez activer Javascript pour la voir.
4
#12 Jun 2009
5
#Licensed under the GPL.
6
#
7
#Look up the files in a folder that contain the BOM marker
8
#and remove the BOM marker from the file
9

    
10
# -- first command line argument must be a folder name
11
folder="$1"
12

    
13
#expand the char codes in the regex to their corresponding characters
14
regex1=$'\xEF\xBB\xBF'
15
cmd1="grep -rl $regex1 $folder"
16

    
17
echo "The following files contain the BOM marker:"
18
$cmd1 
19

    
20
echo "Cleaning up:"
21
$cmd1 | \
22
    while read f; do
23
        #back up file; only if not yet backed up
24
        backup="$f.backup"
25
        if [ ! -e "$backup" ]; then
26
            cp "$f" "$backup"
27
        fi
28
        # the copy operation above could have failed anyway (permissions)
29
        # don't proceed if there is no backup file
30
        if [ -e "$backup" ]; then
31
            #delete the original file
32
            rm -f "$f"
33
            #output the backup file without the BOM
34
            #and write it to the original file location
35
            sed "s/\xEF\xBB\xBF//" "$backup" > "$f"
36
        fi
37
    done 
38

    
39
echo "The following files still contain the BOM marker:"
40
$cmd1