【CentOS 7Shell编程8】,shell中的函数#180228

hellopasswd


shell中的函数

  • 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可 格式: function f_name(){ command } 函数必须要放在最前面
  • 示例1 #!/bin/bash input(){ echo $1 $2 $# $0 }

input 1 a b

[root@localhost shell]# vi 1.sh 
      1 #!/bin/bash
      2 function inp(){
      3     echo $1 $2 $3 $0 $#
      4 }
      5 
      6 inp 1 a 2
[root@localhost shell]# sh 1.sh

1 a 2 1.sh 3
[root@localhost shell]# vi 1.sh 
      1 #!/bin/bash
      2 function inp(){
      3     echo "This number 1 is $1"
      4     echo "This number 2 is $2"
      5     echo "This number 3 is $3"
      6     echo "This number 4 is $0"
      7     echo "This number 5 is $#"
      8 }
      9 
     10 inp b a 2 3 abc

[root@localhost shell]# sh 1.sh 
This number 1 is b
This number 2 is a
This number 3 is 2
This number 4 is 1.sh
This number 5 is 5


[root@localhost shell]# vi 1.sh 
      1 #!/bin/bash
      2 function inp(){
      3     echo "This number 1 is $1"
      4     echo "This number 2 is $2"
      5     echo "This number 3 is $3"
      6     echo "This number 4 is $0"
      7     echo "This number 5 is $#"
      8 }
      9 
     10 inp $1 $2 $3

[root@localhost shell]# sh 1.sh 1
This number 1 is 1
This number 2 is 
This number 3 is 
This number 4 is 1.sh
This number 5 is 1
[root@localhost shell]# sh 1.sh 1 a b
This number 1 is 1
This number 2 is a
This number 3 is b
This number 4 is 1.sh
This number 5 is 3

  • 示例2 #!/bin/bash sum(){ s=$[$1+$2] echo $s } sum 1 2
[root@localhost shell]# vi 1.sh
      1 #!/bin/bash
      2 sum(){
      3     s=$[$1+$2]
      4     echo $s
      5 }
      6 
      7 sum 1 10
[root@localhost shell]# sh 1.sh 
11

  • 示例3

#!/bin/bash ip(){ ifconfig | grep -A1 "$1" | tail -1 | awk '{print $2}' | awk -F':' '{print $2}' } read -p "Please input eth name:" e myip=ip $e echo "$e addreass is $myip"

[root@localhost shell]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.81.130  netmask 255.255.255.0  broadcast 192.168.81.255
        inet6 fe80::20c:29ff:fea5:5a89  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:a5:5a:89  txqueuelen 1000  (Ethernet)
        RX packets 3802  bytes 348661 (340.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2490  bytes 315746 (308.3 KiB)
        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 0  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        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

[root@localhost shell]# ifconfig | grep -A1 "eno16777736:\ " | awk '/inet/ {print $2}'
192.168.81.130

[root@localhost shell]# vi 1.sh 
      1 #!/bin/bash
      2 ip(){
      3     ifconfig | grep -A1 "$1" | awk '/inet/ {print $2}'
      4 }
      5 
      6 read -p "Please input the eth name: " eth
      7 ip $eth
[root@localhost shell]# sh 1.sh 
Please input the eth name: eno16777736
192.168.81.130

判断是否存在网卡,再判断是否网卡是否有ip


修改于 180228

转载于:https://my.oschina.net/hellopasswd/blog/1626621

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值