shell 编程-函数
function (功能) 功能函数
完成特定功能的代码片段
函数必须先定义才能使用
优点:避免重复的代码
定义函数---1.如何定义?
调用函数---如何使用,,分为本地调用,通过别的脚本调用函数。
取消函数----unset func_name
函数传参----和脚本传参类似
命名空间:在shell语言中命名空间函数内和函数外(不包括脚本)是一样的,函数内外不能赋值同样名字的变量
#变量:如果在同一个命名空间可以用,如果不再同一个命名空间就不能用
#分开函数和shell的命名空间:如果不让在其他空间用使用:local 分开
函数变量使用的范围:
默认,函数里的变量会在函数外面生效
local 变量名称 #变量只在函数内生效。属于局部变量
# vim var.sh
#!/usr/bin/bash
a=10
var() {
echo $a
#local a
a=20
echo $a
}
var
echo $a
返回值:return value:#value不能超过0-255,是函数里面函数最后一条执行命令的返回值,默认返回值是由这条命令执行结果确定的
[root@localhost ~]# vim return.sh
#!/usr/bin/bash
func(){
echo "hello"
return 250
if [ $? -eq 0 ];then
return 250 #返回的是执行函数的返回值
else
return 251
fi
}
func
echo $? #返回的是执行命令的返回值
if [ $? -eq 250 ];then
echo "执行成功"
elif [ $? -eq 251 ];then
echo "执行失败"
fi
exit:返回结果并退出程序
return: 返回结果并退出函数
#扩展案例:脚本回滚:
[root@localhost ~]# vim dir.sh
#!/usr/bin/bash
dir(){
for i in $(seq 1 $1)
do
mkdir /opt/dir$i
if [ $? -eq 0 ];then
echo "dir$i创建成功"
sleep 1
else
return 2
fi
done
}
dir $1
echo $?
[root@localhost ~]# bash dir.sh 9
#脚本回滚
[root@localhost ~]# cat dir.sh
#!/usr/bin/bash
dir(){
for i in $(seq 1 $1)
do
mkdir /opt/dir$i
if [ $? -eq 0 ];then
echo "dir$i创建成功"
sleep 1
else
return 2
fi
done
}
func(){
rm -rvf /opt/dir*
}
dir $1
if [ $? -eq 2 ];then
func
else
echo "脚本执行失败"
fi
[root@localhost ~]# bash dir.sh 9
shell 函数function
函数声明
function_name () {
list of commands
}
函数名 function_name,这就是你将使用它从其他地方在你的脚本调用。
取消函数
unset myfunc #取消函数
[root@linux-server script]# vim func.sh
#!/bin/bash
myfunc(){ #定义函数
echo “This is my first shell function”
}
myfunc #函数调用
产生以下执行结果
[root@linux-server script]# bash func.sh
“This is my first shell function”
函数必须提前定义测试
[root@linux-server script]# vim fun01.sh
#!/bin/bash
fun () {
echo "hello"
}
fun
unset fun #取消函数
fun
[root@linux-server script]# bash fun01.sh
hello
fun01.sh: line 7: fun: command not found
函数的返回值,返回的是函数体内最后一条命令是否成功的返回值
[root@linux-server script]# systemctl stop httpd
[root@linux-server script]# cat fun02.sh
#!/bin/bash
fun() {
systemctl status httpd &>/dev/null
systemctl status vsftpd &>/dev/null
}
fun
echo $?
[root@linux-server script]# systemctl stop vsftpd
[root@linux-server script]# bash fun02.sh
3
函数调用之二
定义函数脚本
[root@localhost script]# cat a.sh
#!/usr/bin/bash
check_net() {
echo "正在检查网络通信"
ping -c1 www.baidu.com 2&> /dev/null
if [ $? -eq 0 ];then
echo "你的网络是没有问题的"
else
echo "你的网络有问题,请先检查网络"
exit 2
fi
}
check_net
echo "+++++++++++++++++++++++++++++++++++++++++++++"
check_yum() {
echo "正在检查yum源是否可用"
yum repolist
if [ $? -eq 0 ];then
echo "你的yum源可以正常使用"
else
echo "yum源不能用,请手动配置网络yum源"
exit 3
fi
}
#check_yum
echo "+++++++++++++++++++++++++++++++++++++++++++++++++++"
install_nginx() {
#检查网络是否可以上网
check_net
echo "正在配置nginx的yum源"
cat > /etc/yum.repos.d/nginx.repo <<EOF
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/x86_64/
gpgcheck=0
enabled=1
EOF
yum clean all && sleep 2
yum install -y nginx 2&> /dev/null
if [ $? -eq 0 ];then
echo "nginx install successfull and start nginx"
sleep 2
systemctl start nginx
if [ $? -eq 0 ];then
echo "nginx started is success,plaess check port!"
port=`netstat -lntp | grep nginx | awk '{print $4}' | awk -F ":" '{print $NF}'`
echo "nginx is port $port"
else
echo "nginx 启动失败,请手动检查!"
exit 4
fi
else
echo "nginx install failed!将会自动退出"
exit 5
fi
}
#install_nginx
函数调用
root@localhost script]# cat b.sh #通过其他脚本调用函数脚本
#!/usr/bin/bash
while :
do
echo "这是服务器基本检测功能脚本"
cat <<EOF
++++++++++++++++++++++++++++++++++
+ 1. 检查yum源 +
+ 2. 检查网络 +
+ 3. 安装nginx +
+ 4. 退出 +
++++++++++++++++++++++++++++++++++
EOF
source ./a.sh #写你函数脚本的绝对路径,这里指的是执行函数脚本,让它为下面调用函数生效
read -p "请输入你的选项: " num
case $num in
1)
check_yum
;;
2)
check_net
;;
3)
install_nginx
;;
4)
exit;;
*)
echo "你输入的选项失败请重新输入"
esac
done
函数传参
在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参 数,$2表示第二个参数
示例
[root@linux-server script]# vim fun03.sh
#!/bin/bash
if [ ! $# -eq 3 ];then
echo "Must Input Three number: " p1 p2 p3
exit
fi
fun() {
echo $[$1*$2*$3]
}
fun 1 2 3 #进行传参
[root@linux-server script]# bash fun03.sh 1 3 4 6 #这个时候只是传参到了脚本,并没有传到函数里面
Must Input Three number: p1 p2 p3
修改版:
[root@linux-server script]# vim fun04.sh
fun() {
echo $[$1*$2*$3]
}
fun $1 $2 $3
[root@linux-server script]# bash fun04.sh 1 3 5
15