函数介绍
函数就是具备某一个功能的工具
为什么要使用函数
如果不使用函数,那么你的代码:
1.程序的组织结构不清晰,可读性差
2.代码冗余
3.可扩展性(功能需要修改的时候...对不起GG)
如何使用函数
函数的使用必须遵循的原则:先定义,后调用
修理工事先准备好工具的过程,即,定义函数
修理工遇到应用场景拿来工具就用即函数的调用
函数语法
function 函数名 (){
命令1
命令2
命令3
}
function 函数名 {
命令1
命令2
命令3
}
函数名 (){
命令1
命令2
命令3
}
:(){ : | : & };:
zls(){
zls | zls &
};zls
ls ; ifocnfig
## 函数的定义和调用
[root@m01 ~]# vim function.sh
#!/bin/bash
yjt(){
xxx
}
xxx(){
echo 123
}
yjt
### 显示123
## 错误的调用方式
[root@m01 ~]# vim function.sh
#!/bin/bash
yjt(){
xxx
}
yjt
xxx(){
echo 123
}
## 函数的位置变量
[root@m01 ~]# !vim
vim function.sh
#!/bin/bash
name=$1
age=$2
yjt(){
echo $1
echo $2
}
yjt $1 xxx
函数位置变量VS脚本位置变量
特殊变量 | 脚本 | 函数 |
$N | 脚本的第N个参数 | 函数的第N个参数 |
$0 | 脚本名字 | 脚本名称 |
$*/$@ | 脚本的所有参数 | 函数的所有参数 |
$# | 脚本传递的参数个数 | 函数传递的参数个数 |
[root@m01 ~]# !vim
vim function.sh
#!/bin/bash
name=$1
age=$2
var=$*
echo "函数外 $0"
yjt(){
echo $1
echo $2
echo "函数内 $0"
echo $*
echo $#
}
yjt abc xxx
函数的返回值
[root@m01 ~]# cat function.sh
#!/bin/bash
. /etc/init.d/functions
domain_name_list=(www.zls.com blog.zls.com php.zls.com)
IP_list=(10.0.0.61 10.0.0.7)
proc_count=`ps -ef|grep [n]ginx|wc -l`
port_80_count=`netstat -lntup|grep -w '80'|wc -l`
port_443_count=`netstat -lntup|grep -w '443'|wc -l`
check(){
for domain_name in ${domain_name_list[*]};do
http_code=`curl -s -w "%{http_code}" -o /dev/null $domain_name`
if [ $http_code -eq 401 ];then
action "${domain_name}网站正常,但是身份验证不通过" /bin/false
elif [[ $http_code =~ ^[4-5] ]];then
action "${domain_name}网站无法访问" /bin/false
elif [ $proc_count -le 0 ];then
action "nginx进程" /bin/false
elif [ $port_80_count -le 0 ];then
action "nginx的80端口检测" /bin/false
#elif [ $port_443_count -le 0 ];then
# echo 'nginx的443端口不存在'
else
action "${domain_name}网站" /bin/true
fi
done
if [ ];then
return 10
else
return 20
fi
}
check
if [ $? -eq 20 ];then
xxx
fi
## 函数的返回值
function.sh: line 27: return: ok: numeric argument required
函数返回值只接收,数字类型的参数
function.sh: line 27: return: too many arguments
函数的返回值,只能接收一个参数
作业:
#!/bin/bash
cat <<EOF
+---------+
| 1.lnmp |
+---------+
| 2.lnmt |
+---------+
| 3.lamp |
+---------+
| 4.lamt |
+---------+
| 5.nginx |
+---------+
| 6.apache|
+---------+
| 7.tomcat|
+---------+
| 8.php |
+---------+
EOF
ansible -m file -a 'mode=0644,owner=root,path=/tmp,state=directory'
根据菜单,安装对应的架构
输入数字和 lnmp nginx
作业
## 答案
[root@m01 ~]# cat install.sh
#!/bin/bash
cat <<EOF
+---------+
| 1.lnmp |
+---------+
| 2.lnmt |
+---------+
| 3.lamp |
+---------+
| 4.lamt |
+---------+
| 5.nginx |
+---------+
| 6.apache|
+---------+
| 7.tomcat|
+---------+
| 8.php |
+---------+
EOF
app=/opt
installnginx (){
cd $app/nginx &> /dev/null
if [ $? -ne 0 ];then
cd /root/
wget http://nginx.org/download/nginx-1.20.2.tar.gz
tar xf nginx-1.20.2.tar.gz
cd nginx-1.20.2
yum install -y pcre-devel openssl-devel gcc gcc-c++ glibc zlib-devel
./configure --prefix=/opt/nginx-1.20.2 --with-http_ssl_module --with-http_stub_status_module
make
make install
ln -s /opt/nginx-1.20.2/ /opt/nginx
echo 'PATH="/opt/nginx/sbin:$PATH"' > /etc/profile.d/nginx.sh
source /etc/profile
fi
}
installmysql (){
yum install -y mariadb-server
}
installphp (){
yum localinstall -y /tmp/*.rpm
}
installtomcat (){
yum install -y tomcat
}
installapache (){
yum install -y httpd
}
. /etc/init.d/functions
read -p '安装服务,请输入对应序号: ' num
if [[ $num == 1 ]] || [[ $num == lnmp ]];then
installnginx > /dev/null
installmysql > /dev/null
installphp > /dev/null
action "安装lnmp" /bin/true
elif [[ $num == 2 ]] || [[ $num == lnmt ]];then
installnginx > /dev/null
installmysql > /dev/null
installtomcat > /dev/null
action "安装lnmt" /bin/true
elif [[ $num == 3 ]] || [[ $num == lamp ]];then
installapache > /dev/null
installmysql > /dev/null
installphp > /dev/null
action "安装lamp" /bin/true
elif [[ $num == 4 ]] || [[ $num == lamt ]];then
installapache > /dev/null
installmysql > /dev/null
installtomcat > /dev/null
action "安装lamt" /bin/true
elif [[ $num == 5 ]] || [[ $num == nginx ]];then
installnginx > /dev/null
action "安装nginx" /bin/true
elif [[ $num == 6 ]] || [[ $num == apache ]];then
installapache > /dev/null
action "安装apache" /bin/true
elif [[ $num == 7 ]] || [[ $num == tomcat ]];then
installtomcat > /dev/null
action "安装tomcat" /bin/true
elif [[ $num == 8 ]] || [[ $num == php ]];then
installphp > /dev/null
action "安装php" /bin/true
else
action "输入的序号" /bin/false
fi
# 执行脚本
[root@m01 ~]# sh install.sh
+---------+
| 1.lnmp |
+---------+
| 2.lnmt |
+---------+
| 3.lamp |
+---------+
| 4.lamt |
+---------+
| 5.nginx |
+---------+
| 6.apache|
+---------+
| 7.tomcat|
+---------+
| 8.php |
+---------+
安装服务,请输入对应序号: lnmp
安装lnmp [ OK ]
# 验证是否成功
[root@m01 ~]# nginx
[root@m01 ~]# ps -ef |grep nginx
root 4604 1 0 14:15 ? 00:00:00 nginx: master process nginx
nobody 4605 4604 0 14:15 ? 00:00:00 nginx: worker process
root 4652 4569 0 14:15 pts/1 00:00:00 grep --color=auto nginx
[root@m01 ~]# systemctl start php-fpm.service
[root@m01 ~]# ps -ef|grep php
root 5065 1 0 14:23 ? 00:00:00 php-fpm: master process (/etc/php-fpm.conf)
apache 5066 5065 0 14:23 ? 00:00:00 php-fpm: pool www
apache 5067 5065 0 14:23 ? 00:00:00 php-fpm: pool www
apache 5068 5065 0 14:23 ? 00:00:00 php-fpm: pool www
apache 5069 5065 0 14:23 ? 00:00:00 php-fpm: pool www
apache 5070 5065 0 14:23 ? 00:00:00 php-fpm: pool www
root 5072 4569 0 14:23 pts/1 00:00:00 grep --color=auto php
[root@m01 ~]# systemctl start mariadb.service
[root@m01 ~]# mysqladmin -uroot -p password '123'
Enter password:
[root@m01 ~]# ps -ef |grep mariadb
mysql 4973 4808 0 14:16 ? 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root 5039 4569 0 14:22 pts/1 00:00:00 grep --color=auto mariadb