在 Bash 中使用 {} 范圍表達(dá)式
在編寫 shell 腳本時(shí),有時(shí)需要生成數(shù)字或字符串序列。這種序列數(shù)據(jù)的一種常見(jiàn)用途是用于循環(huán)迭代。
雖然可以使用 seq 之類的專用工具來(lái)生成一系列數(shù)字,但 bash 本身提供大括號(hào)擴(kuò)展,實(shí)際上沒(méi)有必要在 bash 腳本中添加此類外部依賴項(xiàng)。在本教程中,讓我們了解如何使用大括號(hào)擴(kuò)展在 shell 腳本中生成數(shù)據(jù)序列和一些有用的大括號(hào)擴(kuò)展示例。
{}花括號(hào)使用說(shuō)明
Bash 內(nèi)置的 range 函數(shù)是通過(guò)所謂的{}大括號(hào)擴(kuò)展實(shí)現(xiàn)的。簡(jiǎn)而言之,大括號(hào)擴(kuò)展允許根據(jù)提供的字符串和數(shù)字?jǐn)?shù)據(jù)生成字符串序列。大括號(hào)擴(kuò)展的語(yǔ)法如下。
- {<string1>,<string2>,...,<stringN>}
- {<start-number>..<end-number>}
- {<start-number>..<end-number>..<increment>}
- <prefix-string>{......}
- {......}<suffix-string>
- <prefix-string>{......}<suffix-string>
實(shí)例一:列出字符串序列
大括號(hào)擴(kuò)展的第一個(gè)用例是一個(gè)簡(jiǎn)單的字符串列表,它是大括號(hào)內(nèi)以逗號(hào)分隔的字符串列表。這里是簡(jiǎn)單地列出預(yù)定義的字符串?dāng)?shù)據(jù)。
下面使用for循環(huán),列出大括號(hào)中的字符串,如下所示。
- [root@localhost ~]# for fruit in {apple,orange,lemon}; do echo $fruit ; done
- apple
- orange
- lemon
下面實(shí)例是同時(shí)創(chuàng)建多個(gè)子目錄:
- [root@localhost ~]# mkdir -p /tmp/users/{dan,john,alex,michael,emma}
- [root@localhost ~]# ls -l /tmp/users/
- total 0
- drwxr-xr-x 2 root root 6 Aug 6 16:23 alex
- drwxr-xr-x 2 root root 6 Aug 6 16:23 dan
- drwxr-xr-x 2 root root 6 Aug 6 16:23 emma
- drwxr-xr-x 2 root root 6 Aug 6 16:23 john
- drwxr-xr-x 2 root root 6 Aug 6 16:23 michael
下面是創(chuàng)建多個(gè)空文件:
- [root@localhost ~]# touch /tmp/file{1,2,3,4}.log
- [root@localhost ~]# ll /tmp/
- total 0
- -rw-r--r-- 1 root root 0 Aug 6 16:24 file1.log
- -rw-r--r-- 1 root root 0 Aug 6 16:24 file2.log
- -rw-r--r-- 1 root root 0 Aug 6 16:24 file3.log
- -rw-r--r-- 1 root root 0 Aug 6 16:24 file4.log
實(shí)例二:定義一個(gè)數(shù)字范圍
大括號(hào)擴(kuò)展最常見(jiàn)的用例是為循環(huán)迭代定義一個(gè)數(shù)字范圍。你可以使用以下表達(dá)式,在其中指定范圍的開始/結(jié)束,以及可選的增量值。
- {<start-number>..<end-number>}
- {<start-number>..<end-number>..<increment>}
要定義 10 到 20 之間的整數(shù)序列:
- [root@localhost ~]# echo {10..20}
- 10 11 12 13 14 15 16 17 18 19 20
可以在循環(huán)中使用:
- [root@localhost ~]# for num in {10..20}; do echo $num ;done
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
如果要生成 0 到 20 之間增量為 2 的數(shù)字序列:
- [root@localhost ~]# echo {0..20..2}
- 0 2 4 6 8 10 12 14 16 18 20
也可以生成一系列遞減數(shù)字:
- [root@localhost ~]# echo {20..10}
- 20 19 18 17 16 15 14 13 12 11 10
- [root@localhost ~]# echo {20..10..-2}
- 20 18 16 14 12 10
您還可以使用前導(dǎo)零填充數(shù)字,以防需要使用相同數(shù)量的數(shù)字。例如:
- [root@localhost ~]# for num in {00..20..2}; do echo $num ; done
- 00
- 02
- 04
- 06
- 08
- 10
- 12
- 14
- 16
- 18
- 20
實(shí)例三:生成字母序列
大括號(hào)擴(kuò)展不僅可用于生成數(shù)字序列,還可用于生成字母序列:
- [root@localhost ~]# cat brace.sh
- #!/bin/bash
- for char1 in {A..B}; do
- for char2 in {A..B}; do
- echo "${char1}${char2}"
- done
- done
實(shí)例四:生成帶有前綴、后綴的字符串序列
使用此功能,可以輕松生成按順序編號(hào)的文件名列表:
- [root@localhost ~]# for file in img_{00..05}.jpg; do echo $file ; done
- img_00.jpg
- img_01.jpg
- img_02.jpg
- img_03.jpg
- img_04.jpg
- img_05.jpg
實(shí)例五:多個(gè){}花括號(hào)組合使用
最后,可以組合多個(gè)大括號(hào)擴(kuò)展,在這種情況下會(huì)生成所有可能組合。
- for char1 in {A..Z}; do
- for char2 in {A..Z}; do
- echo "${char1}${char2}"
- done
- done
通過(guò)組合兩個(gè)大括號(hào)擴(kuò)展,下面的單個(gè)循環(huán)可以產(chǎn)生與上面相同的輸出。
- for str in {A..Z}{A..Z}; do
- echo $str
- done
總 結(jié)
{}允許您在單個(gè)命令行中輕松生成一系列任意字符串。大括號(hào)擴(kuò)展不僅對(duì) shell 腳本有用,而且在命令行環(huán)境中也很有用。