shell functions and arrays

[tap]

shell functions and arrays

1. Functions in the shell

1.1 Function format 1

function name {
    commands
}

Example 1:

#! /bin/bash

function inp(){             //定义一个inp的函数

echo $1 $2 $3 $0 $#         

}

inp 1 a 2 b                 //传入参数             //传入参数

operation result

[root@xavi ~]# sh function1.sh
1 a 2 function1.sh 4
  • [ ] $1 : The first parameter is "2" as above
  • [ ] $2 : The second parameter is "b" as above
  • [ ] $3 : The third parameter is "3" as above
  • [ ] $0 : The name of the script itself is "function1.sh" as above
  • [ ] $# : In fact, there are several parameters in the statistics that are "2 b 3 c", that is, $# = 4
  • [ ] $@ : represents all parameters 2 b 3 c

1.2 Function format 2

neme() {
    commands
}
  • Example 1
#!/bin/bash

sum() {             //定义的函数名为sum
    s=$[$1+$2]
    echo $s
}
sum 1 2

run

[root@xavi ~]# sh function2.sh
3
  • Example 2

Task: Enter the name of the network card and check the IP address of the network card:

Let's start with normal command debugging:

mark

Finally determined that the valid command is:

ifconfig |grep -A1 "ens33: " |awk '/inet/ {print $2}'

function:

#!/bin/bash
ip()
{
  ifconfig |grep -A1 "$1: " |awk '/inet/ {print $2}'
}

read -p "please input the eth name: "eth
ip $eth

operation result:

[root@xavi ~]# sh funciton3.sh
please input the eth name: eth33
192.168.72.130
192.168.72.150
127.0.0.1
192.168.122.1

Modified complete:

vim funciton3.sh

#!/bin/bash
ip()
{
  ifconfig |grep -A1 "$eth " |awk '/inet/ {print $2}'
}

read -p "please input the eth name: " eth
UseIp=`ip $eth`
echo "$eth adress is $UseIp"

operation result:

[root@xavi ~]# sh funciton3.sh
please input the eth name: ens33: 
ens33: adress is 192.168.72.130
[root@xavi ~]# sh funciton3.sh
please input the eth name: ens33:0:
ens33:0: adress is 192.168.72.150

2. Array variables and functions

2.1 Array operations (array note that the first one is actually a[0], which is different from awk)

[root@xavi ~]# b=(1 2 3 4) //定义一个数组a并赋值 1 2 3
[root@xavi ~]# echo ${b[*]} //注意输出a的值的格式
1 2 3 4
[root@xavi ~]# echo ${b[0]} //注意第一个其实是 b[0]开始
1
[root@xavi ~]# echo ${b[1]}
2
[root@xavi ~]# echo ${b[@]}
1 2 3 4
[root@xavi ~]# echo ${#b[@]} //获取数组的元素个数
4
[root@xavi ~]# echo ${#b[*]} //获取数组的元素个数
4

2.2 Assign value to array, redefine

[root@xavi ~]# b[3]=a
[root@xavi ~]# echo ${b[3]}
a
[root@xavi ~]# echo ${b[*]}
1 2 3 a
[root@xavi ~]# b[4]=a
[root@xavi ~]# echo ${b[*]}
1 2 3 a a

2.3 Deletion of array elements

[root@xavi ~]# unset b[2] //删除摸个数组元素
[root@xavi ~]# echo ${b[*]}
1 2 a a
[root@xavi ~]# unset b  //删除整个数组
[root@xavi ~]# echo ${b[*]}

2.4 Fragmentation of arrays

[root@xavi ~]# a=(`seq 1 10`)
[root@xavi ~]# echo ${a[*]}
1 2 3 4 5 6 7 8 9 10
[root@xavi ~]# echo ${a[@]:3:4} //从第数组a[3]开始,截取4个。
4 5 6 7
[root@xavi ~]# echo ${a[@]:0-3:2} //从倒数第三个数组开始,截取两个
8 9

[root@xavi ~]# echo ${a[@]/8/6} //把8换成6
1 2 3 4 5 6 7 6 9 10  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324691294&siteId=291194637