2024 /7 /26
shell脚本
编写nginx脚本
353 vim nginx.sh //编写nginx脚本
!/bin/bash //shell脚本开头
yum -y install gcc-c++ make pcre-devel openssl-devel wget cd /usr/local/src/ wget 'http://nginx.org/download/nginx-1.22.1.tar.gz' tar xf nginx-1.22.1.tar.gz cd nginx-1.22.1 ./configure --prefix=/usr/local/nginx make -j 4 make install
354 chmod +x nginx.sh //增加执行权限 355 ./nginx.sh //运行脚本 356 systemctl status nginx //查看nginx状态 358 nginx //启动nginx
编写关闭防火墙和selinux脚本
350 vim firewalld.sh #!/bin/bash # 检测是否为 root 用户 if [ "$(id -u)" -ne "0" ]; then echo "请使用 root 用户运行此脚本。" exit 1 fi # 关闭防火墙 echo "正在停止和禁用防火墙..." systemctl stop firewalld systemctl disable firewalld # 检查防火墙状态 if systemctl is-active --quiet firewalld; then echo "防火墙未成功关闭。" else echo "防火墙已成功停止。" fi # 关闭 SELinux echo "正在临时设置 SELinux 为宽松模式..." setenforce 0 # 修改 SELinux 配置文件以永久禁用 if grep -q "^SELINUX=" /etc/selinux/config; then sed -i 's/^SELINUX=.*/SELINUX=permissive/' /etc/selinux/config else echo "SELINUX=permissive" >> /etc/selinux/config fi # 检查 SELinux 状态 if getenforce | grep -q "Permissive"; then echo "SELinux 已成功设置为宽松模式。" else echo "SELinux 设置失败。" fi 351 chmod +x firewalld.sh 352 ./firewalld.sh
创建一个检查网络是否顺畅的脚本
[root@shell ~]# vim net.sh #!/bin/bash read -p "输入网址:" weburl ping -c 3 $weburl &> /dev/null if [ $? -eq 0 ]; then echo "网络畅通" else echo "网络卡死" fi [root@shell ~]# chmod +x net.sh [root@shell ~]# ./net.sh 输入网址:www.baidu.com 网络畅通
写一个增删改查文件的脚本
[root@shell ~]# vim text.sh #!/bin/bash FILE="example.txt" # 增加内容到文件 add_content() { echo "请输入要添加的内容(输入 'exit' 结束):" while read -r line; do if [[ $line == "exit" ]]; then break fi echo "$line" >> "$FILE" done echo "内容已添加到 $FILE" } # 删除文件中的内容 delete_content() { echo "请输入要删除的内容(输入 'exit' 结束):" while read -r line; do if [[ $line == "exit" ]]; then break fi sed -i "/$line/d" "$FILE" done echo "指定内容已从 $FILE 删除" } # 修改文件中的内容 modify_content() { echo "请输入要修改的旧内容:" read -r old_content echo "请输入要修改为的新内容:" read -r new_content sed -i "s/$old_content/$new_content/g" "$FILE" echo "内容已从 '$old_content' 修改为 '$new_content'" } # 查找文件中的内容 search_content() { echo "请输入要查找的内容:" read -r search_term grep "$search_term" "$FILE" } # 显示菜单 show_menu() { echo "请选择操作:" echo "1. 增加内容" echo "2. 删除内容" echo "3. 修改内容" echo "4. 查找内容" echo "5. 退出" } # 主程序 while true; do show_menu read -r choice case $choice in 1) add_content ;; 2) delete_content ;; 3) modify_content ;; 4) search_content ;; 5) echo "退出程序" exit 0 ;; *) echo "无效选项,请重新选择。" ;; esac done [root@shell ~]# chmod +x text.sh [root@shell ~]# ./text.sh 请选择操作: 1. 增加内容 2. 删除内容 3. 修改内容 4. 查找内容 5. 退出
用户登录脚本
[root@shell ~]# vim user.sh #!/bin/bash read -p "请输入账号" user echo $user if [ $user == "admin" ]; then echo "欢迎登录:$user" else echo "账号或密码错误请重试" fi [root@shell ~]# chmod +x user.sh [root@shell ~]# ./user.sh 请输入账号user user 账号或密码错误请重试 [root@shell ~]# ./user.sh 请输入账号admin admin 欢迎登录:admin