Gil*_*il' 8

tail -c 1 输出其输入的最后一个字符(更准确地说,最后一个字节)。

命令替换去除尾随的换行符,因此如果文件的最后一个字符$(tail -c 1 <…)是换行符,则为。如果最后一个字符是空字节(在大多数 shell 中),它也是空的,但文本文件没有空字节。

请记住,空文件不需要额外的换行符。

if [ ! -s "$filename" ]; then
  echo "$filename is empty"
elif [ -z "$(tail -c 1 <"$filename")" ]; then
  echo "$filename ends with a newline or with a null byte"
else
  echo "$filename does not end with a newline nor with a null byte"
fi
Run Code Online (Sandbox Code Playgroud)


DJM*_*hem 0

这其实跟没有什么关系vim。差不多就是你想要的

tail -c 1 file
Run Code Online (Sandbox Code Playgroud)

获取文件的最后一个字符。