Chinaunix首页 | 论坛 | 博客
  • 博客访问: 944459
  • 博文数量: 481
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5078
  • 用 户 组: 普通用户
  • 注册时间: 2018-03-07 14:48
个人简介

分享工作和学习中的点点滴滴,包括前端、后端、运维、产品等各个方面,欢迎您来关注订阅!

文章分类

全部博文(481)

文章存档

2023年(26)

2022年(97)

2021年(119)

2020年(153)

2019年(70)

2018年(16)

我的朋友

分类: LINUX

2022-05-04 02:24:23

在编写  时,有时需要生成数字或字符串序列。这种序列数据的一种常见用途是用于循环迭代。

虽然可以使用 seq 之类的专用工具来生成一系列数字,但 bash 本身提供大括号扩展,实际上没有必要在 bash 中添加此类外部依赖项。在本教程中,让我们了解如何使用大括号扩展在 shell 脚本中生成数据序列和一些有用的大括号扩展示例。

{}花括号使用说明

Bash 内置的 range 函数是通过所谓的{}大括号扩展实现的。简而言之,大括号扩展允许根据提供的字符串和数字数据生成字符串序列。大括号扩展的语法如下。

{,,...,}
{..}
{....}
{......}
{......}
{......}
实例一:列出字符串序列

大括号扩展的第一个用例是一个简单的字符串列表,它是大括号内以逗号分隔的字符串列表。这里是简单地列出预定义的字符串数据。

下面使用for循环,列出大括号中的字符串,如下所示。

[root@localhost ~]# for fruit in {apple,orange,lemon}; do echo $fruit ; done
apple
orange
lemon

如何在bash中使用{}范围表达式如何在bash中使用{}范围表达式
下面实例是同时创建多个子目录:

[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

如何在bash中使用{}范围表达式如何在bash中使用{}范围表达式
下面是创建多个空文件:

[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

如何在bash中使用{}范围表达式如何在bash中使用{}范围表达式

实例二:定义一个数字范围

大括号扩展最常见的用例是为循环迭代定义一个数字范围。你可以使用以下表达式,在其中指定范围的开始/结束,以及可选的增量值。
news.yesky.com/hotnews/311/109240311.shtml
news.yesky.com/hotnews/241/274908741.shtml

{..}
{....}

要定义 10 到 20 之间的整数序列:

[root@localhost ~]# echo {10..20}
10 11 12 13 14 15 16 17 18 19 20

如何在bash中使用{}范围表达式如何在bash中使用{}范围表达式
可以在循环中使用:

[root@localhost ~]# for num in {10..20}; do echo $num ;done
10
11
12
13
14
15
16
17
18
19
20

如何在bash中使用{}范围表达式如何在bash中使用{}范围表达式
如果要生成 0 到 20 之间增量为 2 的数字序列:

[root@localhost ~]# echo {0..20..2}
0 2 4 6 8 10 12 14 16 18 20

如何在bash中使用{}范围表达式如何在bash中使用{}范围表达式
也可以生成一系列递减数字:

[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

如何在bash中使用{}范围表达式如何在bash中使用{}范围表达式
您还可以使用前导零填充数字,以防需要使用相同数量的数字。例如:

[root@localhost ~]# for num in {00..20..2}; do echo $num ; done
00
02
04
06
08
10
12
14
16
18
20

如何在bash中使用{}范围表达式如何在bash中使用{}范围表达式

实例三:生成字母序列

大括号扩展不仅可用于生成数字序列,还可用于生成字母序列:

[root@localhost ~]# cat brace.sh 
#!/bin/bash
for char1 in {A..B}; do
    for char2 in {A..B}; do
        echo "${char1}${char2}"
    done
done

如何在bash中使用{}范围表达式如何在bash中使用{}范围表达式

实例四:生成带有前缀、后缀的字符串序列

使用此功能,可以轻松生成按顺序编号的文件名列表:

[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

如何在bash中使用{}范围表达式如何在bash中使用{}范围表达式

实例五:多个{}花括号组合使用

最后,可以组合多个大括号扩展,在这种情况下会生成所有可能组合。

for char1 in {A..Z}; do
    for char2 in {A..Z}; do
        echo "${char1}${char2}"
    done
done

通过组合两个大括号扩展,下面的单个循环可以产生与上面相同的输出。

for str in {A..Z}{A..Z}; do
    echo $str
done
总结

{}允许您在单个行中轻松生成一系列任意字符串。大括号扩展不仅对 shell 脚本有用,而且在行环境中也很有用。

阅读(5073) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~