linux shell 字典,shell数组与字典总结

[TOC]

> shell里面也有数组和所谓的字典,数组分为索引数组和关联数组(关联数组也就是在其他语言里面的字典,在数组和字典的操作方式基本一致)。

# 一. 数组

## 1.1 创建数组

- 创建空数组

`names=()`

- 数组添加元素

```

names=()

declare -p names

declare -a names=()

names+="beijing"

declare -p names

declare -a names=([0]="beijing")

names+=("beijing" "shanghai" "guangzhou")

declare -p names

declare -a names=([0]="beijing" [1]="beijing" [2]="shanghai" [3]="guangzhou")

```

- 空格分开的字符串用括号包围

`names=("Bob" "Peter" "$USER" "Big Bad John")`

- 指定索引号,索引号不连续的也叫稀疏数组

`names=([0]="Bob" [1]="Peter" [20]="$USER" [21]="Big Bad John")`

- 指定索引号,且单独定义

`names[0]="Bob"`

- 指定路径加通配符

`photos=(~/"My Photos"/*.jpg)`

`files=(*)`

- 显示定义并初始化

`declare -a myfiles='([0]="/home/a/.bashrc" [1]="billing codes.xlsx" [2]="hello.c")'`

- for循环初始化

- 多个数组合并为一个数组

```

names=(beijing shanghai)

ages=(19 20 99)

etcs=(${names[@]} ${ages[@]})

declare -p etcs

declare -a etcs=([0]="beijing" [1]="shanghai" [2]="19" [3]="20" [4]="99")

```

- 数组元素模式匹配(搜索替换元素值)

```

# 精确匹配

names=(beijing shanghai)

new_names=(${names[@]/hai/guangzhou})

declare -p new_names

declare -a new_names=([0]="beijing" [1]="shangguangzhou")

# 模糊匹配

names=(beijing shanghai)

new_names=(${names[@]/*hai/guangzhou})

declare -p new_names

declare -a new_names=([0]="beijing" [1]="guangzhou")

```

## 1.2 访问数组

- 访问数组通常直接用数组下标访问

```

char=(a b c q w x y z)

echo "${char[2]}"

c

```

- 数组的分片访问指定位置

```

# 表示从下标为3的位置开始访问2个元素

arr=(able good fly python java test go now)

echo "${arr[@]:3:2}"

python java

```

- 获取指定下标的元素值的字符个数

```

arr=(able good fly python java test go now)

echo "${#arr[3]}"

6

```

## 1.3 遍历数组

- 打印出变量的类型、值

`declare -p myfiles`

- 使用`printf`循环打印:`${myfiles[@]}`可以理解为`$@`所有的位置参数

`printf '%s\n' "${myfiles[@]}"`

- 显示for循环打印:

```

for file in "${myfiles[@]}"

do

cp "$file" /backups/

done

```

- 使用`"${myfiles[@]}"`代替for循环

```

myfiles=(db.sql home.tbz2 etc.tbz2)

cp "${myfiles[@]}" /backups/

```

- 一次性输出可读性高的字符串连接

```

names=("Bob" "Peter" "$USER" "Big Bad John")

echo "Today's contestants are: ${names[*]}"

Today's contestants are: Bob Peter lhunath Big Bad John

```

- 获取元素个数

```

$ array=(a b c)

$ echo ${#array[@]}

3

declare -a myfiles='([0]=".bashrc" [1]="billing codes.xlsx" [4]="hello.c")'

echo ${#myfiles[@]}

3

```

- 遍历数组的`index`

```

$ first=(Jessica Sue Peter)

$ last=(Jones Storm Parker)

for i in "${!first[@]}"; do

echo "${first[i]} ${last[i]}"

done

结果

Jessica Jones

Sue Storm

Peter Parker

```

- 类似c语言的遍历数组下标,但是步长为2

```

char=(a b c q w x y z)

for ((i=0; i

echo "${char[i]} and ${char[i+1]}"

done

结果

a and b

c and q

w and x

y and z

```

## 1.4 删除元素

```

char=(a b c q w x y z)

declare -p char

declare -a char=([0]="a" [1]="b" [2]="c" [3]="q" [4]="w" [5]="x" [6]="y" [7]="z")

unset char[2]

declare -p char

declare -a char=([0]="a" [1]="b" [3]="q" [4]="w" [5]="x" [6]="y" [7]="z")

```

# 二. 字典

bash里面的字典叫做关联数组,字典其实和数组类似,不同点在于,字典的key是字符串,并且遍历时是随机的。

**注意事项:**

- 字典的key遍历是随机的顺序, 它不适合存放顺序的元素

- 除了遍历是随机的外, 字典的用法基本和数组是一致的

## 2.1 创建字典

- 显示声明并初始化方法1

```

declare -A fullNames

fullNames=( ["lhunath"]="Maarten Billemont" ["greycat"]="Greg Wooledge" )

echo "I'am ${fullNames[greycat]}."

```

- 显示声明并初始化方法2

```

$ declare -A dict

$ dict[astro]="Foo Bar"

$ declare -p dict

declare -A dict='([astro]="Foo Bar")'

```

## 2.2 遍历字典

- 像遍历数组一样,只不过index是字符串了

```

for user in "${!fullNames[@]}"; do

echo "User: $user, full name: ${fullNames[$user]}."

done

User: lhunath, full name: Maarten Billemont.

User: greycat, full name: Greg Wooledge.

```

# 三. 总结

- `for value in "${myfiles[@]}"` 表示遍历数组的元素值

- `for index in "${!myfiles[@]}"` 表示遍历数组的下标号

- `echo "${#myfiles[@]}"` 表示获取数组的元素个数

- `echo "${names[*]}"` 表示一次性的打印成字符串便于阅读

- `declare -a variable` 表示定义一个数组

- `declare -A variable` 表示定义一个字典

- `declare -p variable` 表示打印遍历variable的类型、值

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值