杂:使用Shell判断文件换行符(LF/CRLF)
前提:文件最后一行有换行符
第一步:以二进制方式取得文件最后两个byte。
last2=`tail -c 2 <your_file> | od -x -A n`
第二步:判断最后两个byte是否是'CRLF'
if [ $last2 = 0a0d -o $last2 = 0d0a ]
then
# Cheating! If the file ends in LFCR, it would incorrectly
# say that it is CRLF
echo File ends in CRLF
fi
另外,下面这种方法的原理,让人百思不得其姐,
#重定向标准输入
exec < <your_file>
read -r line
case $line in *$'\r') return 1;; *) return 0;; esac
原以为read加了-r就可以把$'\n'读出来,实际上却是用read把末尾的$'\n'先去掉,剩下的如果是$'\r'就认为是'CRLF'换行。
这个语句表明read一定会去掉末尾的$'\n'
PS:Shell里$'\r'和$'\n'就这么写
完整代码
#!/bin/bash
if [ $# -eq 0 ] ; then
echo "error : there is no para"
exit 1
fi
if [ ! -e $1 ] ; then
echo "file $1 is not exist"
exit 1
fi
last2=$(tail -c 2 "$1" | od -x -A n)
if [ $last2 = 0a0d -o $last2 = 0d0a ] ; then
echo "end with \\r\\n"
else
echo "end with \\n"
fi
遍历文件夹版
#!/bin/bash
#func check CRLF bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
check_CRLF(){
last2=$(tail -c 2 "$1" | od -x -A n)
echo "last2 is $last2"
if [ $last2 = 0a0d -o $last2 = 0d0a ] ; then
echo "warning! file $1 end with \\r\\n"
else
echo "file $1 end with \\n"
fi
}
#func traverse folder bbbbbbbbbbbbbbbbbbbbbbbbbbbb
traverse_folder(){
# no para
if [ $# -eq 0 ]; then
exit 1
fi
# not folder
if [ ! -d $1 ]; then
exit 1
fi
# traversal
for files_or_folder in $1/*
do
# is file
if [ -f $files_or_folder ]; then
check_CRLF $files_or_folder
# is folder
elif [ -d $files_or_folder ]; then
traverse_folder $files_or_folder
else
exit 1
fi
done
}
#bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
if [ $# -eq 0 ] ; then
echo "error : there is no para"
exit
fi
traverse_folder $1
一句搞定版
find . -type f | xargs file | grep CRLF
杂:使用Shell判断文件换行符(LF/CRLF)的更多相关文章
- git 换行符LF与CRLF转换问题
git 换行符LF与CRLF转换问题 一.背景 在各操作系统下,文本文件所使用的换行符是不一样的.UNIX/Linux 使用的是 0x0A(LF),早期的 Mac OS 使用的是0x0D(CR),后来 ...
- shell判断文件是否存在
转自:http://www.cnblogs.com/sunyubo/archive/2011/10/17/2282047.html 1. shell判断文件,目录是否存在或者具有权限 2. #!/bi ...
- Linux shell判断文件和文件夹是否存在
shell判断文件,目录是否存在或者具有权限 #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/acc ...
- shell 判断文件、目录是否存在
shell判断文件是否存在 1. shell判断文件,目录是否存在或者具有权限 2. #!/bin/sh 3. 4. myPath="/var/log/httpd/" 5. m ...
- Shell 判断文件或文件夹是否存在
#shell判断文件夹是否存在 #如果文件夹不存在,创建文件夹 if [ ! -d "/myfolder" ]; then mkdir /myfolder fi #shell判断文 ...
- [转] Linux shell判断文件和文件夹是否存在
shell判断文件,目录是否存在或者具有权限 #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/acc ...
- shell判断文件夹是否存在
#shell判断文件夹是否存在 #如果文件夹不存在,创建文件夹 if [ ! -d "/myfolder" ]; then mkdir /myfolder fi #shell判断文 ...
- shell判断文件类型和权限
shell 判断文件类型. -d 文件 判断该文件是否存在,并且是否为目录(是目录为真) -e文件 判断该文件是否存在(存在为真) -f文件 判断该文件是否存在,并且是否为文件(是普通文件为真) - ...
- shell判断文件是否存在[转]
原文出处: http://canofy.iteye.com/blog/252289 shell判断文件,目录是否存在或者具有权限 #!/bin/sh myPath="/var/log/htt ...
- shell判断文件,目录是否存在或者具有权限
shell判断文件,目录是否存在或者具有权限 #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/ ...
随机推荐
- Zstack 鼎阳SDS6204示波器和Archiver Appliance的重度测试2
https://blog.csdn.net/weixin_43767046/article/details/113748775 https://blog.csdn.net/weixin_4376704 ...
- Zstack EPICS Archiver在小课题组的使用经验
https://www.zstack.io/product/portfolio_comparison/ https://epics-controls.org/resources-and-support ...
- centos7中配置vnc服务多用户
我用的本地yum源. 注意: 在配置root用户时,设置vnc密码.vncpasswd 添加普通用户时,要先在root用户下添加普通用户(如:vnc1,vnc2) useradd vnc1 passw ...
- SSM框架学习-AOP学习笔记
一.AOP入门简介 AOP(Aspect Oriented Programming)面向切面编程,是一种编程范式,可以知道开发者如何组织程序结构 作用:在不惊动原始设计的基础上为其进行功能增强.(无侵 ...
- 在vs code中进行本地调试和开启本地服务器
https://blog.csdn.net/tangxiujiang/article/details/80927699
- 基于docker的spark分布式与单线程、多线程wordcount的对比实验
1. 分布式环境搭建 1.1 基于docker的spark配置文件 docker-compose.yml version: '2' services: spark: image: docker.io/ ...
- pytho获取C函数返回值
python调用C语言接口 注:本文所有示例介绍基于linux平台 在底层开发中,一般是使用C或者C++,但是有时候为了开发效率或者在写测试脚本的时候,会经常使用到python,所以这就涉及到一个问题 ...
- 524. 通过删除字母匹配到字典里最长单词 (Medium)
问题描述 524. 通过删除字母匹配到字典里最长单词 (Medium) 给你一个字符串 s 和一个字符串数组 dictionary ,找出并返回 dictionary 中最长的字符串,该字符串可以通过 ...
- Centos 6.5 iptables 端口白名单设置
iptables -I INPUT -p tcp --dport 8888 -j DROPiptables -I INPUT -s 10.9.145.101 -p tcp --dport 8888 - ...
- PTA---求月天数
最近做了几次模拟考试,对于求月天数这个题目有了更深一点的理解. 这个题的题目基本就是让用户输入年份和月份,给出该月有多少天. 对于这个题,首先就要考虑年份的问题,因为闰年和非闰年在二月是有一点不同的, ...