【shell函数-08】

一、函数:函数的参数:$1第一个参数,$2第二个参数。默认情况下函数里的都是全局变量,要是有局部变量使用local关键字。

1.函数的定义
function 函数名() {
指令
return
}
简化写法1:
function 函数名 {
指令
return
}
简化写法2:【推荐】
函数名() {
指令
return
}

#方法一
function printMsg(){
	echo "hello world"
}
printMsg
#方法二
function printMsg1 {
	echo "hello world1"
}
printMsg1
#方法三【推荐】
printMsg2(){
	echo "hello world2"
}
printMsg2
#带参函数1
printMsg3(){
	echo "$1"
}
printMsg3 "hello world3"
#带参函数2
printMsg4(){
	echo "$1"
}
printMsg4 "$1"
#带参函数3
printMsg(){
	echo "$msg"
}
read -p "input a string..." msg
printMsg $msg
[root@manager day6]# sh f1.sh
hello world
hello world1
hello world2
hello world3
[root@manager day6]# sh f1.sh "hello world4"
hello world
hello world1
hello world2
hello world3
hello world4
[root@manager day6]# sh f1.sh 
hello world
hello world1
hello world2

input a string...23
23

2.调用函数:函数名 参数1 参数2 …
3.函数的返回值:使用return语句来返回某个数值;但是,在Shell中,return语句只能返回某个0~255之间的整数值。一般使用$?来接收函数的返回值
4.案列
练习题1:写一个脚本,判定192.168.0.200-192.168.0.254之间的主机哪些在线

[root@manager day6]# sh fping1.sh
www.12306.cn is up
请输入合法的IP地址:www.baidu.com
www.baidu.com is up
www.12306.cn is up
请输入合法的IP地址:www.baidu.com
www.baidu.com is up
[root@manager day6]# cat fping1.sh 
#!/bin/bash
#*************************************************************
#Author: pyy
#Date:  2020-06-26
#FileName:      fping1.sh
#*************************************************************
#无参数无返回值的函数
PING(){
	if ping www.12306.cn -c 2 -w 0.2 -i 0.1 &>/dev/null
	then
		echo "www.12306.cn is up"
	else
		echo "www.12306.cn is down"
	fi
}
PING
#有参数无返回值
PING(){
	if ping $ip -c 2 -w 0.2 -i 0.1 &>/dev/null
	then
		echo "$ip is up"
	else
		echo "$ip is down"
	fi
}
read -p "请输入合法的IP地址:" ip
PING $ip
#无参数有返回值的函数
PING(){
	if ping www.12306.cn -c 2 -w 0.2 -i 0.1 &>/dev/null
	then
		return 0
	else
		return 2
	fi
}
PING
[ $? -eq 0 ] && echo "www.12306.cn is up" || echo "www.12306.cn is down"
#有参数有返回值
PING(){
	if ping $ip -c 2 -w 0.2 -i 0.1 &>/dev/null
	then
		return 0
	else
		return 2
	fi
}
read -p "请输入合法的IP地址:" ip
PING $ip
[ $? -eq 0 ] && echo "$ip is up" || echo "$ip is down"

练习题2::写一个脚本:使用函数完成
函数能够接受一个参数,参数为用户名;

判断一个用户是否存在
如果存在,就返回此用户的shell和UID;并返回正常状态值;
如果不存在,就说此用户不存在;并返回错误状态值;

[root@manager day6]# cat fuser.sh 
#!/bin/bash
#*************************************************************
#Author: pyy
#Date:  2020-06-26
#FileName:      fuser.sh
#*************************************************************
user(){
	if id $1 &> /dev/null 
	then
		grep ^$1 /etc/passwd | cut -d: -f3,7
		return 0
	else
		return 1
	fi
}
read -p "please input username:" username
until [ "$username" = "q" -o "$username" = "Q" ]; 
do
	user $username
	if [ $? -eq 0 ]
	then
		echo "$username is exists..."
		read -p "please input again:" username
	else
		echo "$username is no exist..."
		read -p "no $username,please input again:" username
	fi
done
[root@manager day6]# sh fuser.sh
please input username:centos
1000:/bin/bash
centos is exists...
please input again:ls
ls is no exist...
no ls,please input again:q

二、函数库文件:为了方便地重用这些功能,可以创建一些可重用的函数。这些函数可以单独地放在函数库文件中。

1.函数库文件定义:
创建一个函数库文件的过程非常类似于编写一个Shell脚本。脚本与库文件之间的唯一区别在于函数库文件通常只包括函数,而脚本中则可以既包括函数和变量的定义,又包括可执行的代码。此处所说的可执行代码,是指位于函数外部的代码,当脚本被载入后,这些代码会立即被执行,毋需另外调用。
2.函数库文件的调用:
当库文件定义好之后,用户就可以在程序中载入库文件,并且调用其中的函数。在Shell中,载入库文件的命令为.,即一个圆点,其语法如下:
. 库文件名称
库文件可以使用相对路径,也可以使用绝对路径。另外,圆点命令和库文件名之间有一个空格

[root@manager day6]# cat fp.sh
#!/bin/bash
#*************************************************************
#Author: pyy
#Date:  2020-06-26
#FileName:      fp.sh
#*************************************************************
[ -f f1.sh ] && . ./f1.sh

printMsg
[root@manager day6]# sh fp.sh
hello world
hello world1
hello world2

input a string...ss
ss
hello world
0
hello world

三、递归函数:自己调用自己

1.练习:递归求阶乘

[root@manager day6]# cat f2.sh 
#!/bin/bash
#*************************************************************
#Author: pyy
#Date:  2020-06-26
#FileName:      f2.sh
#*************************************************************
#
fact(){
local num=$1
local fac
if ((num==1))
then
	fac=1
else
	((dec=num-1))
	fact $dec
	fac=$?
	fac=`expr $num \* $fac`
fi
return $fac
}
fact $1
echo $?
[root@manager day6]# sh f2.sh 5
120
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骑着蜗牛追汤圆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值