shell中的函数

函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字便可。 格式:shell

function f_name() {                   //function是能够省略的,函数名f_name最好不要跟shell中的关键词冲突   
     command         
  }

函数必需要放在最前面,若是调用函数时发现还没定义函数,就会报错vim

示例1

#!/bin/bash 
inp() {
    echo $1 $2 $0 $#   //$0表示脚本名字,$#表示参数个数
}

inp 1 a 2      //使用函数名来调用函数,后面跟须要用的参数

执行结果以下:bash

[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]#

咱们将上面的脚本修改以下:函数

#!/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

执行结果以下:oop

[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]#

咱们再来修改下这个脚本,调用函数的参数也能够使用$1 $2这种形式code

#!/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指的就是给整个脚本传递的参数

执行过程看下面:ip

[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]#

示例2 :两个数之和

#!/bin/bash
sum() {
    s=$[$1+$2]
    echo $s
}
sum 1 10

执行结果以下:input

[root@lijie-01 shell]# sh -x !$
sh -x fun2.sh
+ sum 1 10
+ s=11
+ echo 11
11
[root@lijie-01 shell]#

示例3 输入网卡名字显示网卡IP

分析:好比,我这台虚拟主机有如下网卡io

[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

下面咱们想要经过网卡名字找出对应的IP,咱们就能够经过一个函数来表示ast

#!/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"

咱们来看上面脚本的执行结果

[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 ~]#

下面咱们一步步来看关键代码的执行

[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 ~]#

下面咱们来进一步给上一个shell加入判断条件:判断输入的网卡是否是系统的网卡,判断输入的网卡有没有IP 获得的代码段以下:

相关文章
相关标签/搜索