Shell 学习
因为平时写 Shell 脚本比较多,并且 Shell 的语法规则比较零散,这里做一些学习和实践总结。
Shell 字典
字典的声明1
declare -A all_service
字典的赋值1
all_service=([key1]="value1" [key2]="value2" [key3]="vlaue3")
字典的取值
取 value 值
如取 key1 对应的 value1:1
${all_service[key1]}
如取所有的 value:1
${all_service[*]}
取key值
如取所有的 key:1
${!all_service[*]}
示例1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#!/bin/bash
declare -A all_service
all_service=([ntpd]="on" [lldpad]="on" [firewalld]="off")
echo "---Get value1---"
echo ${all_service[ntpd]}
echo "---Get all values---"
echo ${all_service[*]}
echo "---Get all keys---"
echo ${!all_service[*]}
echo "---Get all key:value pairs---"
for services in $(echo ${!all_service[*]});
do
echo "${services} : ${all_service[${services}]}"
done
运行结果1
2
3
4
5
6
7
8
9
10
11>sh test.sh
---Get value1---
on
---Get all values---
on on off
---Get all keys---
lldpad ntpd firewalld
---Get all key:value pairs---
lldpad : on
ntpd : on
firewalld : off
参考
[1] https://blog.csdn.net/jeremy_yangt/article/details/49100773
[2] https://blog.csdn.net/u014297722/article/details/54601660?utm_source=itdadao&utm_medium=referral