一维数组的定义、统计、引用和删除等操作。
- A=( test1 test2 test3 ) ,定义数组一般以括号的方式来定义, 数组的值可以随机定义。
- echo ${A[0]} ,代表引用第一个数组变量,结果会显示 test1, 数 组引用从 0 开始,代表第一个数组,依次类推。
- echo ${A[1]} ,代表引用第二个数组变量,结果会显示 test2, 数 组引用也是从 0 开始计算的。
- echo ${A[@]} 将显示所有参数 test1 test2 test3 。
- echo ${#A[@]} 将显示该数组的参数 个数 3。
- 替换第二个 test2 数组为 test5 :echo ${A[@]/test2/test5}
- 删除 test3 数组命令为: unset A[2] ;echo ${A[@]}
数组选择某元素
#!/bin/bash soft=( nginx apache php /etc/hosts ) echo "输出soft=${soft[0]}" cat ${soft[3]}
bug记录
#!/bin/bash function nginx_install() { if [ -z `rpm -qa | grep nginx| head -n1` ];then echo "ready to install nginx..." else echo "nginx is installed" exit fi yum install -y nginx && echo "nginx is installed!" } nginx_install
ps:出现问题 [: z: 期待一元表达式 是因为z前面忘记添加-