BOM添加到UTF-8文件 [英] Adding BOM to UTF-8 files
问题描述
我在寻找(没有成功)的脚本,这将工作作为一个批处理文件,并让我prePEND一个UTF-8文本文件,BOM的,如果它不具备的。
I'm searching (without success) a script, which would work as a batch file and allow me to prepend a UTF-8 text file with a BOM if it doesn't have one.
无论它是写在(Perl,Python和C,bash)的语言或它的工作原理上的问题对我的操作系统。我已经获得了广泛的计算机。
Neither the language it is written in (perl, python, c, bash) or the OS it works on matters to me. I have access to a wide range of computers.
我发现了很多脚本做反向(剥离BOM),这对我听起来是一种愚蠢的,因为很多Windows程序将无法读取UTF-8文本文件,如果他们没有一个BOM。
I've found a lot of script to do the reverse (strip the BOM), which sounds to me as kind of silly, as many Windows program will have trouble reading UTF-8 text files if they don't have a BOM.
我错过了明显?
谢谢!
Did I miss the obvious? Thanks!
推荐答案
我使用'文件'命令的 ICU 的'uconv'命令。
I wrote this addbom.sh using the 'file' command and ICU's 'uconv' command.
#!/bin/sh
if [ $# -eq 0 ]
then
echo usage $0 files ...
exit 1
fi
for file in "$@"
do
echo "# Processing: $file" 1>&2
if [ ! -f "$file" ]
then
echo Not a file: "$file" 1>&2
exit 1
fi
TYPE=`file - < "$file" | cut -d: -f2`
if echo "$TYPE" | grep -q '(with BOM)'
then
echo "# $file already has BOM, skipping." 1>&2
else
( mv "${file}" "${file}"~ && uconv -f utf-8 -t utf-8 --add-signature < "${file}~" > "${file}" ) || ( echo Error processing "$file" 1>&2 ; exit 1)
fi
done
编辑:添加围绕报价MV
参数。感谢@DirkR和高兴这个剧本一直如此的帮助!
edit: Added quotes around the mv
arguments. Thanks @DirkR and glad this script has been so helpful!
这篇关于BOM添加到UTF-8文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!