20.16/20.17 shell中的函数 20.18 shell中的数组 20.19 告警系统需求分析
20.16/20.17 shell中的函数 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可。 格式: function f_name() { command } 函数必须要放在最前面 示例1 #!/bin/bash input() { echo $1 $2 $# $0 } input 1 a b $# 参数的个数 $0 参数的名字 函数也支持使用参数 使用方法: sh fun1.sh 参数 参数2 示例2 #!/bin/bash sum() { s=$[$1+$2] echo $s } sum 1 2 示例3 centos6 #!/bin/bash ip() { ifconfig |grep -A1 "$1 " |tail -1 |awk '{print $2}'|awk -F':' '{print $2}' } read -p "Please input the eth name: " e myip=`ip $e` echo "$e address is $myip" centos7 #!/bin/bash ip() { ifconfig |grep -A1 "$1: " | tail -1 | awk '/inet/ {print $2}' } read -p "Please input the eth name: