您的位置 首页 > 腾讯云社区

Linux脚本的bom格式开头---幽鸿

最近在使用shell脚本处理问题的时候,发现脚本莫名其妙的报错,脚本代码如下:

[hduser06@bdphdp010001 0.0.0]$ cat bom.sh ?#!/bin/sh echo 'start export data to sas'

    但是在vi该脚本的时候,却发现:

#!/bin/sh echo 'start export data to sas'

    该脚本是完全正常的,执行的时候报错如下:

[hduser06@bdphdp010001 0.0.0]$ sh bom.sh bom.sh: line 1: ?#!/bin/sh: No such file or directory start export data to sas

    仔细观察下,原来该脚本不小心带了bom文件头。检查一个文件是否带bom头,可以如下检查:

[hduser06@bdphdp010001 0.0.0]$ hexdump -C bom.sh | head 00000000 ef bb bf 23 21 2f 62 69 6e 2f 73 68 0a 0a 65 63 |...#!/bin/sh..ec| 00000010 68 6f 20 27 73 74 61 72 74 20 65 78 70 6f 72 74 |ho 'start export| 00000020 20 64 61 74 61 20 74 6f 20 73 61 73 27 0a 0a | data to sas'..| 0000002f

    这里可以看到bom.sh文件前头有...,这个文件头明显是多余的,这里可以去掉。在windows下可以用notepad和UE都可以去掉。在Linux下,

可以使用如下命令,查出当前所有的带bom的文件列表:

grep -r -I -l $'^xEFxBBxBF' ./

    去掉所有带bom头的文件:

find . -type f -exec sed -i 's/xEFxBBxBF//' {} ;

    重新执行脚本,即可正常执行。

---来自腾讯云社区的---幽鸿

关于作者: 瞎采新闻

这里可以显示个人介绍!这里可以显示个人介绍!

热门文章

留言与评论(共有 0 条评论)
   
验证码: