【1121】shell(下)
5.39 函数
5.40 shell 数组
数组赋值
数组的删除
数组分片
数组替换
5.39 函数
函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可。
格式:
function f_name() {
command
}
函数必须要放在最前面
示例脚本1:
[root@alexis-01 shell]# vim fun1.sh //定义 f_name 最好不要和 shell 里面的关键词冲突
#!/bin/bash
inp(){
echo $1 $2 $3 $0 $#
}
inp 1 a 2
[root@alexis-01 shell]# sh fun1.sh
1 a 2 fun1.sh 3
$1 $2 第一个,第二个参数
$# 脚本名称
$0 参数数量
将脚本更改为下面内容,测试结果
#!/bin/bash
inp(){
echo "The first par is $1"
echo "The second par is $2"
echo "The third par is $3"
echo "The script name is $0"
echo "The quantity of par is $#"
}
inp 1 a 2 d kjf
[root@alexis-01 shell]# sh fun1.sh
The first par is 1
The second par is a
The third par is 2
The script name is fun1.sh
The quantity of par is 5
参数可以写在脚本之外
[root@alexis-01 shell]# vim fun1.sh
#!/bin/bash
inp(){
echo "The first par is $1"
echo "The second par is $2"
echo "The third par is $3"
echo "The script name is $0"
echo "The quantity of par is $#"
}
inp $1 $2 $3
这是指 inp 的第一个、第二、第三个参数
[root@alexis-01 shell]# sh fun1.sh 1 3 //参数写在脚本外,名称后面,也行
The first par is 1
The second par is 3
The third par is
The script name is fun1.sh
The quantity of par is 2
示例脚本2:
[root@alexis-01 shell]# vim fun2.sh
#!/bin/bash
sum (){
s=$[$1+$2]
echo $s
}
sum 1 10
[root@alexis-01 shell]# sh -x fun2.sh
+ sum 1 10
+ s=11
+ echo 11
11
示例脚本3:
[root@alexis-01 shell]# vim fun3.sh //输入网卡名字,来显示网卡ip
#!/bin/bash
ip() {
ifconfig |grep -A1 "$1: " |awk '/inet/ {print $2}'
}
read -p "Please input the eth name: " eth
ip $eth
[root@alexis-01 shell]# sh fun3.sh
Please input the eth name: ens33
192.168.194.130
[root@alexis-01 shell]# sh fun3.sh
Please input the eth name: ens37
192.168.100.1
[root@alexis-01 shell]# sh fun3.sh
Please input the eth name: ens38
192.168.174.100
grep -A1 显示关键行及关键行的下一行
课后作业:
输入网卡名,判断是不是空,是不是系统中的网卡
思路: 首先解决输入为空的问题,如果输入内容为空,就提示要输入内容并重新循环,
其次要是系统中存在的网卡,而网卡配置文件在/etc/sysconfig/network-scripts/下,并且都以ifcfg-为开头,那么就可以以此判断输入的网卡名为名称的"ifcfg-网
5.40 shell 数组
定义数组 a=(1 2 3 4 5); echo ${a[@]} 数组不一定要是数字
echo ${a[*]} 等同于 ${a[@]} 显示整个数组
[root@alexis-01 shell]# b=(1 2 3)
[root@alexis-01 shell]# echo ${b[@]} //可以用 @ 或者 *
1 2 3
[root@alexis-01 shell]# echo ${b[*]}
1 2 3
echo ${a[2]} 读取第三个元素,数组从0开始
[root@alexis-01 shell]# echo ${b[1]}
2
[root@alexis-01 shell]# echo ${b[2]}
3
[root@alexis-01 shell]# echo ${b[0]}
1
echo ${#a[@]} 获取数组的元素个数
[root@alexis-01 shell]# echo ${#b[*]}
3
数组赋值,更改,覆盖
a[1]=100; echo ${a[@]}
[root@alexis-01 shell]# b[3]=a
[root@alexis-01 shell]# echo ${b[*]}
1 2 3 a
可以更改已经赋值的数组为新的数值
[root@alexis-01 shell]# b[3]=aaa
[root@alexis-01 shell]# echo ${b[*]}
1 2 3 aaa
·a[5]=2; echo ${a[@]} 如果下标不存在则会自动添加一个元素
[root@alexis-01 shell]# b[5]=ccc
[root@alexis-01 shell]# echo ${b[*]}
1 2 3 aaa ccc
[root@alexis-01 shell]# echo ${b[4]}
[root@alexis-01 shell]#
数组的删除
uset a; unset a[1]
[root@alexis-01 shell]# unset b[5]
[root@alexis-01 shell]# echo ${b[*]}
1 2 3 aaa
[root@alexis-01 shell]# echo ${b[4]}
[root@alexis-01 shell]# unset b
[root@alexis-01 shell]# echo ${b[*]}
数组分片
·a=(seq 1 10)
·echo ${a[@]:0:3} 从第一个元素开始,截取3个
[root@alexis-01 shell]# c=(`seq 1 10`)
[root@alexis-01 shell]# echo ${c[*]}
1 2 3 4 5 6 7 8 9 10
要截取4到7,就是第3个元素开始,截取4个元素
[root@alexis-01 shell]# echo ${c[*]:3:4}
4 5 6 7
要截取倒数第3个元素开始,截取2个元素
[root@alexis-01 shell]# echo ${c[*]:0-3:2}
8 9
数组替换
·echo ${a[@]/3/100} 替换的是值
·a=(${a[@]/3/100})
[root@alexis-01 shell]# echo ${c[*]}
1 2 3 4 5 6 7 8 9 10
把8替换成6
[root@alexis-01 shell]# echo ${c[*]/8/6}
1 2 3 4 5 6 7 6 9 10
直接替换赋值
[root@alexis-01 shell]# a=(${c[*]/4/99})
[root@alexis-01 shell]# echo ${a[*]}
1 2 3 99 5 6 7 8 9 10
来源:oschina
链接:https://my.oschina.net/u/4095803/blog/4467360