The function is to organize a piece of code into a small unit, and give this small unit a name, when you use this code, you can directly call the name of this small unit. Format:
function f_name() { //function是可以省略的,函数名f_name最好不要跟shell中的关键词冲突
command
}
The function must be placed at the top. If the function is called and it is found that the function has not been defined, an error will be reported.
Example 1
#!/bin/bash
inp() {
echo $1 $2 $0 $# //$0表示脚本名字,$#表示参数个数
}
inp 1 a 2 //使用函数名来调用函数,后面跟需要用的参数
The execution result is as follows:
[root@lijie-01 shell]# sh -x fun1.sh
+ inp 1 a 2
+ echo 1 a fun1.sh 3
1 a fun1.sh 3
[root@lijie-01 shell]#
We modify the above script as follows:
#!/bin/bash
function inp(){
echo " The first par is $1"
echo " The second par is $2"
echo " The script name is $0"
echo " The numbers of par is $#"
}
inp 1 a 2 b
The execution result is as follows:
[root@lijie-01 shell]# sh -x fun1.sh
+ inp 1 a 2 b
+ echo ' The first par is 1'
The first par is 1
+ echo ' The second par is a'
The second par is a
+ echo ' The script name is fun1.sh'
The script name is fun1.sh
+ echo ' The numbers of par is 4'
The numbers of par is 4
[root@lijie-01 shell]# sh fun1.sh
The first par is 1
The second par is a
The script name is fun1.sh
The numbers of par is 4
[root@lijie-01 shell]#
Let's modify the script again. The parameters of the calling function can also be in the form of $1 $2
#!/bin/bash
function inp(){
echo " The first par is $1"
echo " The second par is $2"
echo " The script name is $0"
echo " The numbers of par is $#"
}
inp $1 $2 //这里的$1 $2指的就是给整个脚本传递的参数
The execution process is as follows:
[root@lijie-01 shell]# sh -x fun1.sh //不带参数执行,返回的结果如下
+ inp
+ echo ' The first par is '
The first par is
+ echo ' The second par is '
The second par is
+ echo ' The script name is fun1.sh'
The script name is fun1.sh
+ echo ' The numbers of par is 0'
The numbers of par is 0
[root@lijie-01 shell]# sh -x fun1.sh 1 //带1个参数执行,返回的结果如下
+ inp 1
+ echo ' The first par is 1'
The first par is 1
+ echo ' The second par is '
The second par is
+ echo ' The script name is fun1.sh'
The script name is fun1.sh
+ echo ' The numbers of par is 1'
The numbers of par is 1
[root@lijie-01 shell]#
Example 2: Sum of two numbers
#!/bin/bash
sum() {
s=$[$1+$2]
echo $s
}
sum 1 10
The execution result is as follows:
[root@lijie-01 shell]# sh -x !$
sh -x fun2.sh
+ sum 1 10
+ s=11
+ echo 11
11
[root@lijie-01 shell]#
Example 3 Enter the name of the network card to display the IP of the network card
Analysis: For example, my virtual host has the following network cards
[root@lijie-01 shell]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.75.136 netmask 255.255.255.0 broadcast 192.168.75.255
inet6 fe80::8ace:f0ca:bb6e:d1f0 prefixlen 64 scopeid 0x20<link>
inet6 fe80::d652:b567:6190:8f28 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:21:5e:c0 txqueuelen 1000 (Ethernet)
RX packets 196221 bytes 15057853 (14.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 191762 bytes 39138157 (37.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.75.150 netmask 255.255.255.0 broadcast 192.168.75.255
ether 00:0c:29:21:5e:c0 txqueuelen 1000 (Ethernet)
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:21:5e:ca txqueuelen 1000 (Ethernet)
RX packets 1018 bytes 101956 (99.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 800280 bytes 230505908 (219.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 800280 bytes 230505908 219.8 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Next, we want to find the corresponding IP through the name of the network card, we can express it through a function
#!/bin/bash
ip() {
ifconfig |grep -A1 "$1: " |grep 'inet' |awk '{print $2}' //-A1显示关键词的这一行及下一行,这行代码的含义在下个代码块解析
}
read -p "Please input the eth name: " e
myip=`ip $e`
echo "$e address is $myip"
Let's look at the execution result of the above script
[root@lijie-01 shell]# sh fun3.sh
Please input the eth name.ens33
192.168.75.136
[root@lijie-01 shell]# vim fun3.sh
[root@lijie-01 shell]# sh fun3.sh
Please input the eth name.ens33:0
192.168.75.150
[root@lijie-01 shell]# sh fun3.sh //由于ens37没有IP,因此没有输出
Please input the eth name.ens37
[root@lijie-01 ~]#
Let's take a step-by-step look at the execution of the key code
[root@lijie-01 ~]# ifconfig |grep "ens33" //过滤出包含ens33的行,结果出现两行,不是我们想要的
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
[root@lijie-01 ~]# ifconfig |grep "ens33: " //给过滤词后面增加: 来精准识别到我们想要的行
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
[root@lijie-01 ~]# ifconfig |grep -A1 "ens33: " //加上-A1会显示我们识别到的行及其下一行
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.75.136 netmask 255.255.255.0 broadcast 192.168.75.255
[root@lijie-01 ~]# ifconfig |grep -A1 "ens33: " |grep 'inet' //将上一步过滤出来的结果选择包含inet的行
inet 192.168.75.136 netmask 255.255.255.0 broadcast 192.168.75.255
[root@lijie-01 ~]# ifconfig |grep -A1 "ens33: " |grep 'inet' |awk '{print $2}' //将上面的结果的第二段打印出来
192.168.75.136
[root@lijie-01 ~]#
Let's further add judgment conditions to the previous shell: to judge whether the input network card is a system network card, and to judge whether the input network card has an IP. The code segment obtained is as follows: