如何检测换行符文件结束? [英] How to detect file ends in newline?

查看:124
本文介绍了如何检测换行符文件结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在在<一个href=\"http://stackoverflow.com/questions/38664/can-you-modify-text-files-when-committing-to-subversion\"相对=nofollow>致力于颠覆时,您可以修改文本文件? <一个href=\"http://stackoverflow.com/questions/38664/can-you-modify-text-files-when-committing-to-subversion#38666\"相对=nofollow>捐赠建议我阻止,而不是承诺。

Over at Can you modify text files when committing to subversion? Grant suggested that I block commits instead.

不过,我不知道如何检查以新行的文件结束。你怎么能检测到该文件以换行符结束?

However I don't know how to check a file ends with a newline. How can you detect that the file ends with a newline?

推荐答案

@康拉德 :尾巴不返回一个空行。我由具有一些文本中不换行结束,并且做了文件的一个文件。下面是从尾部的输出:

@Konrad: tail does not return an empty line. I made a file that has some text that doesn't end in newline and a file that does. Here is the output from tail:

$ cat test_no_newline.txt
this file doesn't end in newline$ 

$ cat test_with_newline.txt
this file ends in newline
$

虽然我发现尾部有获得最后一个字节的选择。所以我修改你的脚本:

Though I found that tail has get last byte option. So I modified your script to:

#!/bin/sh
c=`tail -c 1 $1`
if [ "$c" != "" ]; then echo "no newline"; fi

这篇关于如何检测换行符文件结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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