自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Bash技巧:把變量賦值為換行符,判斷文件是否以換行符結(jié)尾

系統(tǒng) Linux
在 bash 中,如果要把變量賦值為換行符,寫為 '\n' 沒(méi)有效果,需要寫為 $'\n'。一起來(lái)看看吧。

 [[338354]]

把變量賦值為換行符

在 bash 中,如果要把變量賦值為換行符,寫為 '\n' 沒(méi)有效果,需要寫為 $'\n'。具體舉例如下: 

  1. newline='\n'  
  2. $ echo $newline  
  3. \n  
  4. newline=$'\n'  
  5. $ echo $newline 

可以看到,把 newline 變量賦值為 'n',得到的是 n 這個(gè)字符串,而不是換行符自身。

這是 bash 和 C 語(yǔ)言不一樣的地方。

在 C 語(yǔ)言中,'n' 對(duì)應(yīng)換行符自身,只有一個(gè)字符;而 "n" 對(duì)應(yīng)一個(gè)字符串。

但是在 bash 中,'n' 也是對(duì)應(yīng)一個(gè)字符串。

把 newline 賦值為 $'\n',就能獲取到換行符自身。查看 man bash 對(duì)這個(gè)寫法的說(shuō)明如下:

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: 

  1. \n     new line  
  2. \r     carriage return  
  3. \t     horizontal tab  
  4. \'     single quote 

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

即,$'string' 這個(gè)寫法可以使用 C 語(yǔ)言的轉(zhuǎn)義字符來(lái)獲取到對(duì)應(yīng)的字符自身。

判斷文件的最后一行是否以換行符結(jié)尾

在 Linux 中,可以使用下面命令來(lái)判斷文件的最后一行是否以換行符結(jié)尾: 

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

這里使用 tail filename -c 1 命令獲取到 filename 文件的最后一個(gè)字符。

實(shí)際使用時(shí),需要把 filename 換成具體要判斷的文件名。

tail 命令可以獲取文件末尾的內(nèi)容。它的 -c 選項(xiàng)指定要獲取文件末尾的多少個(gè)字節(jié)。

查看 man tail 對(duì) -c 選項(xiàng)的說(shuō)明如下:

-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 命令指定獲取所給文件的最后一個(gè)字符。

獲取到文件的最后一個(gè)字符后,要判斷該字符是不是換行符。這里不能直接判斷該字符是否等于換行符,而是要判斷該字符是否為空。

原因在于,使用 $(tail filename -c 1) 命令替換來(lái)獲取內(nèi)部命令的輸出結(jié)果時(shí),bash 會(huì)去掉末尾的換行符。

所以當(dāng)文件的最后一行以換行符結(jié)尾時(shí),$(tail filename -c 1) 命令替換會(huì)去掉獲取到的換行符,最終結(jié)果為空,并不會(huì)返回?fù)Q行符自身。

查看 man bash 對(duì)命令替換(command substitution)的說(shuō)明如下

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

  1. $(command)  
  2. or  
  3. `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.

可以看到,經(jīng)過(guò)命令替換后,會(huì)去掉末尾的換行符。

由于 $(tail filename -c 1) 命令替換會(huì)去掉末尾的換行符,這里使用 test -n 來(lái)判斷最終結(jié)果是否為空字符串。

如果文件最后一行以換行符結(jié)尾,那么 $(tail filename -c 1) 的結(jié)果為空,test -n 命令會(huì)返回 1,也就是 false。

如果文件最后一行沒(méi)有以換行符結(jié)尾,那么 $(tail filename -c 1) 的結(jié)果不為空,test -n 命令會(huì)返回 0,也就是 true。

可以根據(jù)實(shí)際需要,改用 test -z 來(lái)判斷。如果文件最后一行以換行符結(jié)尾,$(tail filename -c 1) 的結(jié)果為空,test -z 命令會(huì)返回 0,也就是 true。 

 

責(zé)任編輯:龐桂玉 來(lái)源: segmentfault
相關(guān)推薦

2009-09-02 14:47:44

C#換行符

2009-09-03 15:50:20

C#回車換行符

2010-09-28 16:02:46

替換SQL字段

2011-01-26 11:19:55

Linux認(rèn)證

2010-08-12 10:26:12

FlexXML

2021-10-19 07:27:08

Unix Dos轉(zhuǎn)換

2021-12-14 07:05:00

SQL語(yǔ)句數(shù)據(jù)庫(kù)

2009-09-02 17:19:43

C#換行連接符

2011-08-11 18:19:32

col中文man

2017-10-24 14:13:56

Java正則表達(dá)式

2009-09-02 14:30:35

C#換行符號(hào)

2023-10-20 15:58:27

Python刪除指定字符

2021-05-11 22:24:56

C++符號(hào)Tab

2009-07-06 15:23:18

JSP換行

2022-03-08 09:09:08

Go塊讀取音視頻

2009-11-26 14:23:10

PHP正則模式修正符

2022-11-03 08:13:52

echo 命令Linux

2010-01-07 18:32:38

JSON

2010-01-18 10:34:21

C++編譯器

2024-04-16 00:11:05

Python換行符測(cè)試
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)