anaconda可以做网站吗,上海网站建设管理,室内设计的公司有哪些,荣耀手机官方网站首页函数 1.定义函数2.调用函数2.1.取消函数2.2.其他脚本调用 3.函数传参 1.定义函数
函数声明#xff1a; function_name () { list of commands } 函数名 function_name#xff0c;这就是你将使用它从其他地方在你的脚本调用。 function (功能) 功能函数
计算机函数… 函数 1.定义函数2.调用函数2.1.取消函数2.2.其他脚本调用 3.函数传参 1.定义函数
函数声明 function_name () { list of commands } 函数名 function_name这就是你将使用它从其他地方在你的脚本调用。 function (功能) 功能函数
计算机函数就是固定的模块、固定的功能做固定的操作相当于linux中的固定的功能当需要使用的时候就去调用函数作用完成特定功能的代码片段函数必须先定义才能使用优点避免重复的代码命名空间在shell语言中命名空间函数内和函数外(不包括脚本)是一样的函数内外不能赋值同样名字的变量变量如果在同一个命名空间同一个脚本内可以用如果不再同一个命名空间就不能用分开函数和shell的命名空间如果不让在其他空间用使用local 分开函数变量使用的范围 默认函数里的变量会在函数外面生效全局变量 local 变量名称变量只在函数内生效属于局部变量
[rootlocalhost ~]# vim test.sh
a10 # 全局变量
func1() {local b20 # 局部变量echo $bc30 # 全局变量
}
func1 #调用函数
echo $a # 10
echo $b # 错误,b是func1的局部变量
echo $c # 30
//a 是全局变量能在整个脚本内用
//b 是func1的局部变量只在func1内可用
//c 是全局变量定义在func1内但属于整个脚本的变量
[rootlocalhost ~]# bash test.sh
20
10
30总结 在同一命名空间(同一个shell脚本)内定义的变量能全局使用 局部变量只在函数定义它的地方有效出了这个函数就无法使用。 返回值return value value不能超过0-255是函数里面函数最后一条执行命令的返回值默认返回值是由这条命令执行结果确定的
[rootlocalhost ~]# vim re.sh
#!/usr/bin/bash
func(){echo hello# return 250 返回250并退出了函数下面的语句不执行if [ $? -eq 0 ];thenreturn 250 #返回的是执行函数的返回值elsereturn 251fi
}
func1(){echo hello
}
func #调用函数func
echo $? #打印状态码看看是否为250
func1 #调用函数func1
echo $? #打印状态码看看是否为0
//执行
[rootlocalhost ~]# bash re.sh
hello
250
hello
0总结 exit返回结果并退出程序 return返回结果并退出函数 函数的返回值返回的是函数体内【最后一条命令】是否成功的返回值
[rootlocalhost ~]# systemctl stop httpd
[rootlocalhost ~]# systemctl stop vsftpd
[rootlocalhost ~]# vim fun02.sh
#!/bin/bash
fun() {systemctl status httpd /dev/nullsystemctl status vsftpd /dev/null
}
fun
echo $?
[rootlocalhost ~]# bash fun02.sh
32.调用函数
2.1.取消函数
取消函数 unset myfunc
[rootlocalhost ~]# vim fun01.sh
#!/bin/bash
fun () {
echo hello
}
fun #调用函数
unset fun #取消函数
fun #再次调用函数
[rootlocalhost ~]# bash fun01.sh
hello
fun01.sh: line 7: fun: command not found2.2.其他脚本调用
[rootlocalhost ~]# cat a.sh
#!/usr/bin/bash
check_net() {echo 正在检查网络通信ping -c1 www.baidu.com 2 /dev/nullif [ $? -eq 0 ];thenecho 你的网络是没有问题的elseecho 你的网络有问题请先检查网络exit 2fi
}
check_net
echo check_yum() {echo 正在检查yum源是否可用yum repolistif [ $? -eq 0 ];thenecho 你的yum源可以正常使用elseecho yum源不能用请手动配置网络yum源exit 3fi
}
#check_yum
echo install_nginx() {
#检查网络是否可以上网
check_net
echo 正在配置nginx的yum源
cat /etc/yum.repos.d/nginx.repo EOF
[nginx-stable]
namenginx stable repo
baseurlhttp://nginx.org/packages/centos/7/x86_64/
gpgcheck0
enabled1
EOF
yum clean all sleep 2
yum install -y nginx 2 /dev/null
if [ $? -eq 0 ];thenecho nginx install successfull and start nginxsleep 2systemctl start nginxif [ $? -eq 0 ];thenecho nginx started is success,plaess check port!portnetstat -lntp | grep nginx | awk {print $4} | awk -F : {print $NF}echo nginx is port $portelseecho nginx 启动失败请手动检查!exit 4fi
elseecho nginx install failed!将会自动退出exit 5
fi
}
#install_nginx//函数调用
[rootlocalhost ~]# cat b.sh #通过其他脚本调用函数脚本
#!/usr/bin/bash
while :
do
echo 这是服务器基本检测功能脚本
echo 1. 检查yum源 2. 检查网络 3. 安装nginx 4. 退出 source ./a.sh #写你函数脚本的绝对路径这里指的是执行函数脚本让它为下面调用函数生效
read -p 请输入你的选项: num
case $num in1)check_yum;;2)check_net;;3)install_nginx;;4)exit;;*)echo 你输入的选项失败请重新输入
esac
done3.函数传参
在Shell中调用函数时可以向其传递参数。在函数体内部通过 $n 的形式来获取参数的值例如$1表示第一个参数$2表示第二个参数
[rootlocalhost ~]# vim fun03.sh
#!/bin/bash
if [ ! $# -eq 3 ];thenecho Must Input Three number: p1 p2 p3
exit
fi
fun() {echo $[$1*$2*$3]
}
fun 1 2 3 #进行传参[rootlocalhost ~]# bash fun03.sh 1 3 4 6 //这个时候只是传参到了脚本并没有传到函数里面
Must Input Three number: p1 p2 p3//修改版:
[rootlocalhost ~]# vim fun04.sh
fun() {echo $[$1*$2*$3]
}
fun $1 $2 $3[rootlocalhost ~]# bash fun04.sh 1 3 5
15