与 Cygwin 和一些最近的 Linux 发行版一起安装的最新 (7.1) 版本的dos2unix (和unix2dos ) 命令有一个方便的--info选项,可以打印出每个文件中不同类型的换行符的计数。这是 dos2unix 7.1 (2014-10-06) http://waterlan.home.xs4all.nl/dos2unix.html
从手册页:
--info[=FLAGS] FILE ...
Display file information. No conversion is done.
The following information is printed, in this order:
number of DOS line breaks, number of Unix line breaks, number of Mac line breaks, byte order mark, text or binary, file name.
Example output:
6 0 0 no_bom text dos.txt
0 6 0 no_bom text unix.txt
0 0 6 no_bom text mac.txt
6 6 6 no_bom text mixed.txt
50 0 0 UTF-16LE text utf16le.txt
0 50 0 no_bom text utf8unix.txt
50 0 0 UTF-8 text utf8dos.txt
2 418 219 no_bom binary dos2unix.exe
Optionally extra flags can be set to change the output. One or more flags can be added.
d Print number of DOS line breaks.
u Print number of Unix line breaks.
m Print number of Mac line breaks.
b Print the byte order mark.
t Print if file is text or binary.
c Print only the files that would be converted.
With the "c" flag dos2unix will print only the files that contain DOS line breaks, unix2dos will print only file names that have Unix line breaks.
因此:
if [[ -n $(dos2unix --info=c "${filename}") ]] ; then echo DOS; fi
反过来:
if [[ -n $(unix2dos --info=c "${filename}") ]] ; then echo UNIX; fi