Shell编程函数实战
- Shell允许将一组命令集或语句形成一个可用块,这些块称为Shell函数,Shell函数的用于在于只需定义一次,后期随时使用即可,无需在Shell脚本中添加重复的语句块,其语法格式以function name(){开头,以}结尾。
- Shell编程函数默认不能将参数传入()内部,Shell函数参数传递在调用函数名称传递,例如name args1 args2
function name (){
command1
command2
........
}
name args1 args2
函数语句Shell脚本编程案例如下:
- SHELL编程开发Nginx多版本软件安装脚本
要求如下:
- 支持多版本的安装
- 支持创建多个虚拟主机
- 支持删除多个虚拟主机
#!/bin/bash
NGX_YUM="yum install -y"
NGX_DIR="/usr/local/nginx"
NGX_URL="http://nginx.org/download"
NGX_ARGS="--user=www --group=www --with-http_stub_status_module"
function nginx_help(){
echo -e "\033[33mNginx VIrtual Manager SHELL Scripts\033[0m"
echo -e "\033[33m-------------------------------\033[0m"
echo -e "\033[33m1)-I 1.12.2|1.15.0|1.16.0\033[0m"
echo -e "\033[33m2)-A v1.jfedu.net|v2.jfedu.net v3.jfedu.net\033[0m"
echo -e "\033[33m3)-D v1.jfedu.net|v2.jfedu.net v3.jfedu.net\033[0m"
echo -e "\033[35mUsage:{/bin/bash $0 -I(Install) -A(add) or -D(del) -H(help)\033[0m"
exit 0
}
if [ $
nginx_help
exit 1
fi
if [ ! -f $NGX_DIR/sbin/nginx ];then
$NGX_YUM wget gzip tar make gcc
$NGX_YUM pcre pcre-devel zlib-devel
cd /usr/src
wget -c ${NGX_URL}/nginx-$*.tar.gz
ls -l nginx-$*.tar.gz
tar -xzvf nginx-$*.tar.gz
cd nginx-$*/
useradd -s /sbin/nologin www -M
./configure --prefix=$NGX_DIR/ $NGX_ARGS
make
make install
$NGX_DIR/sbin/nginx
ps -ef|grep nginx
netstat -tnlp|grep 80
setenforce 0
systemctl stop firewalld.service
else
echo -e "\033[33m-----------------------\033[0m"
echo -e "\033[32mThe Nginx WEB Already Install,Please Exit.\033[0m"
echo "ls -l $NGX_DIR"
ls -l $NGX_DIR
fi
}
function config_vhost(){
cd $NGX_DIR/conf/
\cp nginx.conf nginx.conf.bak
grep "vhost" nginx.conf >/dev/null
if [ $? -ne 0 ];then
cat>nginx.conf<<-EOF
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include /usr/local/nginx/conf/vhost/*.conf
}
EOF
mkdir -p $NGX_DIR/conf/vhost
fi
for NGX_VHOST in $*
do
cd $NGX_DIR/conf/vhost/
if [ ! -f ${NGX_VHOST}.conf ];then
cat>${NGX_VHOST}.conf <<-EOF
server {
listen 80;
server_name $NGX_VHOST;
location / {
root html/$NGX_VHOST;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
EOF
mkdir -p $NGX_DIR/html/$NGX_VHOST/
cd $NGX_DIR/html/$NGX_VHOST/
cat>index.html<<-EOF
<html>
<h1>$NGX_VHOST Test Pages</h1>
<hr color=red>
</html>
EOF
echo -e "\033[32mThe $NGX_VHOST Add success.\033[0m"
echo -e "\033[32m---------------\033[0m"
cat $NGX_DIR/conf/domains/$NGX_VHOST
echo -e "\033[32m---------------\033[0m"
$NGX_DIR/sbin/nginx -t
$NGX_DIR/sbin/nginx -s reload
else
echo -e "\033[32mThe $NGX_VHOST already exists.\033[0m"
echo -e "\033[32m---------------\033[0m"
cat $NGX_DIR/conf/domains/$NGX_VHOST
fi
done
}
function delete_vhost(){
for NGX_VHOST in $*
do
echo -e "\033[32m---------------\033[0m"
ls -l|grep "$NGX_VHOST"
cat $NGX_DIR/conf/vhost/${NGX_VHOST}.conf
if [ $? -eq 0 ];then
read -p "Are you sure you want to delete it,yes or no? " INPUT
case $INPUT in
yes|YES|Yes|y|Y)
rm -rf $NGX_DIR/conf/vhost/${NGX_VHOST}.conf
rm -rf $NGX_DIR/html/$NGX_VHOST
echo -e "\033[32m---------------\033[0m"
echo -e "\033[32mThe $NGX_VHOST delete success.\033[0m"
$NGX_DIR/sbin/nginx -s reload
;;
* )
exit 0
;;
esac
fi
done
}
case $1 in
-I )
shift 1
nginx_install $*
;;
-A )
shift 1
config_vhost $*
;;
-D )
shift 1
delete_vhost $*
;;
* )
nginx_help
;;
esac