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 獲得的代碼段以下:

相關文章
相關標籤/搜索