关于linux:Bash技巧把变量赋值为换行符判断文件是否以换行符结尾

7次阅读

共计 1791 个字符,预计需要花费 5 分钟才能阅读完成。

把变量赋值为换行符

在 bash 中,如果要把变量赋值为换行符,写为 '\n' 没有成果,须要写为 $'\n'。具体举例如下:

$ newline='\n'
$ echo $newline
\n
$ newline=$'\n'
$ echo $newline

能够看到,把 newline 变量赋值为 ‘n’,失去的是 n 这个字符串,而不是换行符本身。

这是 bash 和 C 语言不一样的中央。
在 C 语言中,’n’ 对应换行符本身,只有一个字符;而 “n” 对应一个字符串。
然而在 bash 中,’n’ 也是对应一个字符串。

newline 赋值为 $'\n',就能获取到换行符本身。查看 man bash 对这个写法的阐明如下:

Words of the form $’string’ are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:

    \n     new line
    \r     carriage return
    \t     horizontal tab
    \'     single quote

The expanded result is single-quoted, as if the dollar sign had not been present.

即,$'string' 这个写法能够应用 C 语言的转义字符来获取到对应的字符本身。

判断文件的最初一行是否以换行符结尾

在 Linux 中,能够应用上面命令来判断文件的最初一行是否以换行符结尾:

test -n "$(tail filename -c 1)"

这里应用 tail filename -c 1 命令获取到 filename 文件的最初一个字符。

理论应用时,须要把 filename 换成具体要判断的文件名。

tail 命令能够获取文件开端的内容。它的 -c 选项指定要获取文件开端的多少个字节。

查看 man tail 对 -c 选项的阐明如下:

-c, –bytes=K

output the last K bytes; alternatively, use -c +K to output bytes starting with the Kth of each file.

即,tail -c 1 命令指定获取所给文件的最初一个字符。

获取到文件的最初一个字符后,要判断该字符是不是换行符。这里不能直接判断该字符是否等于换行符,而是要判断该字符是否为空。

起因在于,应用 $(tail filename -c 1) 命令替换来获取外部命令的输入后果时,bash 会去掉开端的换行符。

所以当文件的最初一行以换行符结尾时,$(tail filename -c 1) 命令替换会去掉获取到的换行符,最终后果为空,并不会返回换行符本身。

查看 man bash 对命令替换(command substitution)的阐明如下:

Command substitution allows the output of a command to replace the command name. There are two forms:

        $(command)
    or
        `command`

Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting.

能够看到,通过命令替换后,会去掉开端的换行符。

因为 $(tail filename -c 1) 命令替换会去掉开端的换行符,这里应用 test -n 来判断最终后果是否为空字符串。

如果文件最初一行以换行符结尾,那么 $(tail filename -c 1) 的后果为空,test -n 命令会返回 1,也就是 false。

如果文件最初一行没有以换行符结尾,那么 $(tail filename -c 1) 的后果不为空,test -n 命令会返回 0,也就是 true。

能够依据理论须要,改用 test -z 来判断。如果文件最初一行以换行符结尾,$(tail filename -c 1) 的后果为空,test -z 命令会返回 0,也就是 true。

正文完
 0