Customizing a function for printing function parameters
[[email protected] shell]# vim fun1.sh#!/bin/bashpri(){ echo "第一个参数为 $1" echo "第二个参数为 $2" echo "文件名为 $0" echo "总共有$#个参数" }pri a b #调用函数
Execution results
[[email protected] shell]# sh fun1.sh第一个参数为 a第二个参数为 b文件名为 fun1.sh总共有2个参数
Customize a function that will add calculations
[[email protected] shell]# vim fun2.sh#!/bin/bashsum (){ s=$[$1+$2] echo $s }sum 6 9
Execution results
[[email protected] shell]# sh -x fun2.sh+ sum 6 9+ s=15+ echo 1515
Customize an input network card name to display IP functions
[[email protected] shell]# vim fun3.sh#!/bin/baship (){ a=`ifconfig |grep -A1 "$1: "` b=`ifconfig |grep -A1 "$1: "|awk ‘/inet/ {print $2}‘` if [ -z "$a" ] then echo "网卡不存在" exit elif [ -z "$b" ] then echo "网卡没有定义ip" exit else echo "$b" fi}read -p "请输入网卡名:" ethip $eth
Execution results
[[email protected] shell]# sh fun3.sh请输入网卡名:ens33192.168.130.116[[email protected] shell]# sh fun3.sh请输入网卡名:ens37网卡没有定义ip[[email protected] shell]# sh fun3.sh请输入网卡名:ens38网卡不存在
Custom functions in the shell