将 BOM 添加到 UTF-8 文件 [英] Adding BOM to UTF-8 files

查看:32
本文介绍了将 BOM 添加到 UTF-8 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索(但没有成功)一个脚本,它可以作为批处理文件使用,如果没有 BOM,我可以在 UTF-8 文本文件前面加上 BOM.

I'm searching (without success) for 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) nor the OS it works on, matters to me. I have access to a wide range of computers.

我发现有很多脚本可以做相反的事情(去除 BOM),这在我看来有点傻,因为许多 Windows 程序如果没有物料清单.

I've found a lot of scripts 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?

谢谢!

推荐答案

我使用 'file' 命令和 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

edit:mv 参数周围添加引号.感谢@DirkR,很高兴这个脚本很有帮助!

edit: Added quotes around the mv arguments. Thanks @DirkR and glad this script has been so helpful!

这篇关于将 BOM 添加到 UTF-8 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆