Files
Linux/Shell/Shell脚本集合记录.md
offends cee91802b3
Some checks failed
continuous-integration/drone Build is failing
synchronization
2025-08-25 15:57:40 +08:00

1.7 KiB
Raw Permalink Blame History

本文作者:丁辉

Shell脚本集合记录

Shell里有 Read 如何默认回车?

  • 示例一

    #!/bin/bash
    
    defaultValue="默认值"
    
    # 如果没有输入,使用默认值
    read userInput
    userInput=${userInput:-$defaultValue}
    
    echo "你输入的内容是:$userInput"
    

    执行脚本

    bash shell.sh <<< ""
    
  • 示例二

    如果有很多 read 需要默认回车

    #!/bin/bash
    
    defaultValue1="默认值1"
    defaultValue2="默认值2"
    
    echo "请输入内容1"
    read userInput1
    userInput1=${userInput1:-$defaultValue1}
    
    echo "请输入内容2"
    read userInput2
    userInput2=${userInput2:-$defaultValue2}
    
    echo "你输入的内容1是$userInput1"
    echo "你输入的内容2是$userInput2"
    

    执行脚本

    bash shell.sh <<< $'\n\n'
    

如何模拟按钮选择

Shell文件

下载 Shell 示例

wget https://gitee.com/offends/Linux/raw/main/File/Shell/button.sh

Nginx证书签发

Shell文件

下载 Shell 示例

wget https://gitee.com/offends/Linux/raw/main/File/Shell/nginx-ssl.sh

Mysql定时备份脚本

  1. 创建备份文件存储目录

    mkdir -p /opt/mysql/backup && cd /opt/mysql/backup
    
  2. 下载脚本

    wget https://gitee.com/offends/Linux/raw/main/File/Shell/mysql-backup.sh
    

    修改如下内容

    MYSQL_HOST=<MYSQL_HOST>
    MYSQL_USER=root
    MYSQL_PASS=root
    
  3. 配置计划任务

    crontab -e
    

    添加定时任务

    0 2 * * * bash /opt/mysql/backup/mysql-backup.sh