shell脚本中的函数
函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可。格式: function f_name() { command }函数必须要放在最前面
示例1
#!/bin/bash
input() {
echo $1 $2 $# $0
}
input 1 a b
示例2
#!/bin/bash
sum() {
s=$[$1+$2]
echo $s
}
sum 1 2
示例3
#!/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"