110 lines
1.7 KiB
Markdown
110 lines
1.7 KiB
Markdown
> 本文作者:丁辉
|
||
|
||
# Shell脚本集合记录
|
||
|
||
## Shell里有 Read 如何默认回车?
|
||
|
||
- 示例一
|
||
|
||
```bash
|
||
#!/bin/bash
|
||
|
||
defaultValue="默认值"
|
||
|
||
# 如果没有输入,使用默认值
|
||
read userInput
|
||
userInput=${userInput:-$defaultValue}
|
||
|
||
echo "你输入的内容是:$userInput"
|
||
```
|
||
|
||
执行脚本
|
||
|
||
```bash
|
||
bash shell.sh <<< ""
|
||
```
|
||
|
||
- 示例二
|
||
|
||
> 如果有很多 read 需要默认回车
|
||
|
||
```bash
|
||
#!/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
|
||
bash shell.sh <<< $'\n\n'
|
||
```
|
||
|
||
|
||
## 如何模拟按钮选择
|
||
|
||
[Shell文件](https://gitee.com/offends/Linux/blob/main/File/Shell/button.sh)
|
||
|
||
下载 Shell 示例
|
||
|
||
```bash
|
||
wget https://gitee.com/offends/Linux/raw/main/File/Shell/button.sh
|
||
```
|
||
|
||
## Nginx证书签发
|
||
|
||
[Shell文件](https://gitee.com/offends/Linux/blob/main/File/Shell/nginx-ssl.sh)
|
||
|
||
下载 Shell 示例
|
||
|
||
```bash
|
||
wget https://gitee.com/offends/Linux/raw/main/File/Shell/nginx-ssl.sh
|
||
```
|
||
|
||
## Mysql定时备份脚本
|
||
|
||
1. 创建备份文件存储目录
|
||
|
||
```bash
|
||
mkdir -p /opt/mysql/backup && cd /opt/mysql/backup
|
||
```
|
||
|
||
2. 下载脚本
|
||
|
||
```bash
|
||
wget https://gitee.com/offends/Linux/raw/main/File/Shell/mysql-backup.sh
|
||
```
|
||
|
||
修改如下内容
|
||
|
||
```bash
|
||
MYSQL_HOST=<MYSQL_HOST>
|
||
MYSQL_USER=root
|
||
MYSQL_PASS=root
|
||
```
|
||
|
||
3. 配置计划任务
|
||
|
||
```bash
|
||
crontab -e
|
||
```
|
||
|
||
添加定时任务
|
||
|
||
```bash
|
||
0 2 * * * bash /opt/mysql/backup/mysql-backup.sh
|
||
```
|
||
|
||
|