Adding utf-8 Byte Order Mark to file
Thu, 09/19/2013 - 15:57 — sandip#!/bin/bash
# add_bom.sh
FILE=$1
check() {
if (( e = "$1" )); then
echo "ERROR: $ACTION failed!!"
exit $e
else
echo "INFO: $ACTION Success!!"
fi
}
set_bom() {
ACTION="Setting utf-8 BOM"
# Detect if BOM already exists
BOM=`sed -n '1{/^\xEF\xBB\xBF/p}' ; $FILE`
if [ -z $BOM ]; then
&nbs p; [ -f "$FILE" ] && /usr/bin/vim -e -s +"set bomb|set encoding=utf-8|wq" $FILE
&nbs p; check $?
fi
}
set_bom
To run, give the script the file name as argument:
sh ./add_bom.sh /path/to/file
Test the file via xxd, if the mark is present, the first line should output as below:
$ xxd </path/to/file
0000000: efbb bf...