前提:文件最后一行有换行符

第一步:以二进制方式取得文件最后两个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)的更多相关文章

  1. git 换行符LF与CRLF转换问题

    git 换行符LF与CRLF转换问题 一.背景 在各操作系统下,文本文件所使用的换行符是不一样的.UNIX/Linux 使用的是 0x0A(LF),早期的 Mac OS 使用的是0x0D(CR),后来 ...

  2. shell判断文件是否存在

    转自:http://www.cnblogs.com/sunyubo/archive/2011/10/17/2282047.html 1. shell判断文件,目录是否存在或者具有权限 2. #!/bi ...

  3. Linux shell判断文件和文件夹是否存在

    shell判断文件,目录是否存在或者具有权限 #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/acc ...

  4. shell 判断文件、目录是否存在

    shell判断文件是否存在   1. shell判断文件,目录是否存在或者具有权限 2. #!/bin/sh 3. 4. myPath="/var/log/httpd/" 5. m ...

  5. Shell 判断文件或文件夹是否存在

    #shell判断文件夹是否存在 #如果文件夹不存在,创建文件夹 if [ ! -d "/myfolder" ]; then mkdir /myfolder fi #shell判断文 ...

  6. [转] Linux shell判断文件和文件夹是否存在

    shell判断文件,目录是否存在或者具有权限 #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/acc ...

  7. shell判断文件夹是否存在

    #shell判断文件夹是否存在 #如果文件夹不存在,创建文件夹 if [ ! -d "/myfolder" ]; then mkdir /myfolder fi #shell判断文 ...

  8. shell判断文件类型和权限

    shell  判断文件类型. -d 文件 判断该文件是否存在,并且是否为目录(是目录为真) -e文件 判断该文件是否存在(存在为真) -f文件 判断该文件是否存在,并且是否为文件(是普通文件为真) - ...

  9. shell判断文件是否存在[转]

    原文出处: http://canofy.iteye.com/blog/252289 shell判断文件,目录是否存在或者具有权限 #!/bin/sh myPath="/var/log/htt ...

  10. shell判断文件,目录是否存在或者具有权限

    shell判断文件,目录是否存在或者具有权限  #!/bin/sh  myPath="/var/log/httpd/"  myFile="/var /log/httpd/ ...

随机推荐

  1. uboot启动过程 2

    512M内存, 地址范围 [0x80000000, 0xA0000000)  UBOOT原先位置 0x87800000, 移动后的位置0x9FF47000,   也就是最后 700多k,  前面的位置 ...

  2. Zstack使用经验系列1-安装的网络配置

    https://www.zstack.io/help/product_manuals/maintenance_manual/4.html 在官网上从这里有详细的安装,读者可以从上面链接开始起步装起来. ...

  3. JZOJ 1967.【2011集训队出题】聪聪可可

    题目 [2011集训队出题]聪聪可可 思路 看看做做 阴阳 这道题 极力推荐 自从做了这道题后,这些题就变成秒切的题了 很容易想到求节点到分治中心的距离,然后 \(\bmod 3\) 那么在求根节点一 ...

  4. JZOJ 4318. 【NOIP2015模拟11.5】俄罗斯套娃

    题目大意 求逆序对个数小于等于 \(k\) 的排列数 解析 已经做过很多次了,经典得不能再经典的问题 注意本题很卡空间,要用滚动数组 \(Code\) #include<cstdio> u ...

  5. EMBARK研究: 依那西普治疗早期nr-axSpA达48周的临床和MRI疗效

    关键词: 放射学阴性中轴型SpA; TNF拮抗剂; 磁共振影像 EMBARK研究48周结果: 依那西普治疗早期放射学阴性中轴型SpA患者的临床与MRI疗效 电邮发布日期:2016年1月25日 文献: ...

  6. LeetCode-851 喧嚣与富有

    来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/loud-and-rich 题目描述 有一组 n 个人作为实验对象,从 0 到 n - 1 编号, ...

  7. pat乙级 1019 数字黑洞

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> ...

  8. Pytorch和torchvision版本号对应表

    torch与torchvision版本对应表 torch及torchvision版本号查询 import torch print(torch.__version__) import torchvisi ...

  9. 学习笔记3:Android Studio 配置NDK编译c++代码

    NDK编译c++代码有两个方式: 1  ndk-build.cmd + Android.mk + Application.mk 编译, 可单独用ndk编译, 不使用IDE,使用Android需要配置b ...

  10. 类型提示和python函数中'->'的用法

    一.类型提示 在python中,我们定义一个有参函数,调用该函数时需要传入参数,如下所示: # 定义一个简单的函数 def get_full_name(first_name, last_name): ...