UTF-8 (with BOM) and UTF-8 conversion

Eclipse uses UTF-8 BOM format encoding, while Linux uses UTF-8 without BOM format. BOM is used to mark the byte order of the encoding, but because the encoding byte order is irrelevant for UTF-8, and the Unicode standard does not recommend using the UTF-8 encoding format with BOM. In short, due to the difference between Windows and Linux, in order to avoid the tedious use of Notepad ++ to manually change a large number of files, the shell conversion script is provided as follows:

UTF-8(with BOM)转UTF-8:

#!/bin/bash
#将UTF-8带BOM编码的文件转化为UTF-8无BOM格式
if [[ -z "$1" ]];then
    echo '用法:./rmbom.sh [folder | file]'
    echo '将UTF-8编码的文件转化为UTF-8无BOM格式'
    exit 1
fi
 
 
path=$1
find $path -type f -name "*" -print | xargs -i sed -i '1 s/^\xef\xbb\xbf//' {}
echo "Convert finish"

UTF-8转UTF-8(with BOM):

#!/bin/bash
#将UTF-8无BOM编码的文件转化为UTF-8带BOM格式
 
 
if [[ -z "$1" ]];then
    echo '用法:./addbom.sh [folder | file]'
    echo '将UTF-8无BOM格式编码的文件转化为UTF-8带BOM'
    exit 1
fi
 
 
path=$1
find $path -type f -name "*" -print | xargs -i sed -i '1 s/^/\xef\xbb\xbf&/' {}
echo "Convert finish"

Guess you like

Origin www.cnblogs.com/MessiXiaoMo3334/p/12757961.html