shell编程-2

1 位置变量

$num

范例:

[root@newrain shell]# cat weizhi.sh 
#!/bin/bash
#...
echo 我的第一个位置参数是:$1 
echo 我的第二个位置参数是:$2 
echo 我的第三个位置参数是:$3 
echo 我的第四个位置参数是:$4 
echo 一共有 $# 个位置参数 
echo 你输入的参数分别是:$*

$# 统计位置参数总数

$* 列出输入的参数

$0 打印当前执行的脚本名

2 四则运算

let

let a=1+1

echo let

#!/bin/bash
a=$(1:-0)	#a为位置1变量,默认值为0
b=$(2:-0)	#b为位置2变量,默认值为0
echo $((a-b))
let a=10-5
let b=10+$a
let c=$a+$b
echo "c的值为:$c" 

3 ping网络中的for&if

for i in {1..254}
do
	ping $ip$i
{if [$? = 0];then
	echo “$ip$i”  >>ip_up.txt
else
	echo "$ip$i" >>ip_down.txt
fi}& 
# 将if判断放到后台执行
done

# **面**题**,一个服务器有多个ip,然后让指定ip ping

ping -S 10.36.178.2 baidu.com	#-S source 源ip

4 shell自动化部署qqfarm

1.0

#!/bin/bash
########################################################################
# File Name: lnmp_install.sh
# install LNMP 1.0
########################################################################

echo "修改主机名"
hostnamectl set-hostname $

echo "关闭防火墙和selinux"
systemctl stop firewalld --now
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=/disabled/' /etc/selinux/config

echo "配置ali源
rm -rf /etc/yum.repos.d/*
curl -o /root/yum-server.sh http://10.36.178.200/yum-server.sh >> /dev/null
sh /root/yum-server.sh >> /dev/null
yum clean all >> /dev/null
yum makecache >> /dev/null

echo '安装初始化工具'
yum install -y wget vim unzip ntpdate >>/dev/null




echo "配置静态ip"
sed -i 's/BOOTPROTO="dhcp"/BOOTPROTO="static"/' /etc/sysconfig/network-scripts/ifcfg-ens33
cat >> /etc/sysconfig/network-scripts/ifcfg-ens33 <<EOF
IPADDR=...
PREFIX=24
GATEWAY=...
DNS1=223.5.5.5
DNS2=223.6.6.6
EOF

systemctl restart network

ping -c4 www.baidu.com &>> /dev/null
if [ $? -eq 0 ];then
    echo "
else
    echo "ping不通baidu,不通"
fi

echo "配置nginx官方yum"
cat >> /etc/yum.repos.d/nginx.repo <<EOF
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
echo "安装nginx"
read -p '请输入要安装的nginx版本' nginxVersion
yum install -y nginx-${nginxVersion:-1.20.1} &> /dev/null
systemctl start nginx
 && systemctl enable nginx

echo "安装mysql"
yum install -y mysql-server >/dev/null
systemctl start mysqld && systemctl enable mysqld

read -s -p '请输入数据库新的密码:' mysqlPassword
mysqladmin -p"`awk '/t 'emporary password/{p=$NF}END{print p}' /var/log/mysqld.log`" password "$mysqlPassword"

echo "安装php"
read -p '请输入要安装的php版本' phpVersion
yum install -y ${phpVersion}php ${phpVersion}php-fpm ${phpVersion}php-curl ${phpVersion}php-intl ${phpVersion}php-mcrypt ${phpVersion}php-mysql ${phpVersion}php-mbstring ${phpVersion}php-xml ${phpVersion}php-dom ${phpVersion}php-gd >> /dev/null

systemctl start ${phpVersion}php-fpm

echo "修改nginx配置文件"
yum install -y ${phpVersion}php ${phpVersion}php-fpm ${phpVersion}php-curl ${phpVersion}php-intl ${phpVersion}php-mcrypt ${phpVersion}php-mysql ${phpVersion}php-mbstring ${phpVersion}php-xml ${phpVersion}php-dom ${phpVersion}php-gd >> /dev/null




2.0

#!/bin/bash
########################################################################
# File Name: lnmp_install.sh
# install LNMP 2.0
########################################################################
while 1>0
do
cat <<EOF
##################################################
#             1.init                             #
#             2.install Nginx (eg: 1.20.1 1.24.1)#
#             3.install Mysql                    #
#             4.install PHP(eg: php74- php80-)   #
#             5.config nginx                     #
#             6.test nginx php                   #
#             7.quit                             #
##################################################
EOF
read -p "请输入编号选择要做的事情:" var
init() {
read -t 5 -p '请输入主机名:' hostname
ip=`ip a |grep inet |grep ens33 |awk '{print $2}'|awk -F'/' '{print $1}'`
hostnamectl set-hostname ${hostname:-chaosaigc}
systemctl stop firewalld --now
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

#rm -rf /etc/yum.repos.d/*
curl -o /root/yum-server.sh http://10.36.178.200/yum-server.sh &>> /dev/null
sh /root/yum-server.sh >> /dev/null
yum clean all &> /dev/null
yum makecache &> /dev/null
yum install -y wget vim unzip ntpdate net-tools >>/dev/null
ntpdate time.windows.com >/dev/null


# 判断是否已配置静态ip,如已配置则跳过
grep 'IPADDR' /etc/sysconfig/network-scripts/ifcfg-ens33
if [ $? -ne 0 ];then
sed -i 's/BOOTPROTO="dhcp"/BOOTPROTO="static"/' /etc/sysconfig/network-scripts/ifcfg-ens33
gateway=`route -n |awk 'NR==3{print $2}'`
cat >> /etc/sysconfig/network-scripts/ifcfg-ens33 <<EOF
IPADDR=$ip
PREFIX=24
GATEWAY=$gateway
DNS1=223.5.5.5
DNS2=223.6.6.6
EOF
systemctl restart network
fi
}

install_nginx() {
# 判断nginx.repo是否已配置,如已配置则跳过
if [ ! -f /etc/yum.repos.d/nginx.repo ];then
cat >> /etc/yum.repos.d/nginx.repo <<EOF
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
fi
echo "已配置nginx官方yum源"

# 判断是否已安装nginx,如已安装则跳过
nginx -v &>/dev/null
if [ $? -ne 0 ];then
read -t 5 -p '请输入要安装的nginx版本: ' nginxVersion
yum install -y nginx-${nginxVersion:-1.20.1} &> /dev/null
systemctl start nginx && systemctl enable nginx
fi
}

install_mysql() {
# 判断是否已安装mysql,如已安装则跳过
mysql --version &> /dev/null
if [ $? -ne 0 ];then
yum install -y mysql-server >/dev/null
systemctl start mysqld && systemctl enable mysqld

read -t 5 -s -p '请输入数据库新的密码: ' mysqlPassword
mysqladmin -p"`awk '/temporary password/{p=$NF}END{print p}' /var/log/mysqld.log`" password "${mysqlPassword:-QianFeng@123}" &>/dev/null
fi
}

install_php() {
# 判断是否已安装php,如已安装则跳过
ss -tlnp |grep php &>/dev/null
if [ $? -ne 0 ];then
read -t 5 -p '请输入要安装的php版本: ' phpVersion
yum install -y ${phpVersion}php ${phpVersion}php-fpm ${phpVersion}php-curl ${phpVersion}php-intl ${phpVersion}php-mcrypt ${phpVersion}php-mysql ${phpVersion}php-mbstring ${phpVersion}php-xml ${phpVersion}php-dom ${phpVersion}php-gd >> /dev/null

systemctl start ${phpVersion}php-fpm
fi
}


config_nginx() {
# 判断nginx是否安装,如果已安装,则进行配置
nginx -v &>/dev/null
if [ $? -eq 0 ];then
cat > /etc/nginx/nginx.conf <<EOF
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '\$remote_addr - \$remote_user [\$time_local] "\$request" '
                      '\$status \$body_bytes_sent "\$http_referer" '
                      '"\$http_user_agent" "\$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        index  index.php index.html index.htm;
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html\$fastcgi_script_name;
            include        fastcgi_params;
        }
 
        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

}
EOF
rm -rf /etc/nginx/conf.d/* &>/dev/null
systemctl restart nginx
fi
}

test_nginx_php() {
rm -rf /usr/share/nginx/html/*
cat >> /usr/share/nginx/html/index.php <<EOF
<?php
phpinfo();
?>
EOF
}
main() {
    init
    install_nginx
    install_mysql
    install_php
    config_nginx
    test_nginx_php
}

case $var in
1)
    init
    ;;
2)
    install_nginx
    ;;
3)
    install_mysql
    ;;
4)
    install_php
    ;;
5)
    config_nginx
    ;;
6)
    test_nginx_php
    ;;
7)
    echo "退出循环"
    break
    ;;
*)
    echo "开始执行步骤1-6"
    main;
    ;;
esac
done
  • 18
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值