sh 是 shell scripting 的缩写,它是一种脚本语言,用于自动化日常任务和执行命令行操作。shell 脚本可以运行在任何支持 sh 的操作系统上,如 linux、macos、windows 等。
以下是一些使用 sh 编写的示例脚本:
1. 文件操作:
```sh
#!/bin/sh
# 创建一个新文件
touch newfile.txt
# 删除一个已存在的文件
rm newfile.txt
# 复制一个文件到另一个位置
cp oldfile.txt newfile.txt
# 重命名一个文件
mv oldfile.txt newname.txt
# 列出当前目录下的所有文件和文件夹
ls -l
```
2. 系统管理:
```sh
#!/bin/sh
# 设置环境变量
export PATH=/usr/local/bin:$PATH
# 启动一个服务
systemctl start myservice
# 停止一个服务
systemctl stop myservice
# 重启一个服务
systemctl restart myservice
# 检查服务状态
systemctl status myservice
```
3. 网络操作:
```sh
#!/bin/sh
# ping 目标地址
ping www.google.com
# 发送 ssh 连接请求
ssh user@remotehost
# 查看远程主机上的文件列表
ls -l remotehost/path/to/file
# 下载文件
wget http://example.com/file.zip
# 上传文件
rsync -avz source_file destination_file
```
4. 数据库操作:
```sh
#!/bin/sh
# 连接到本地数据库
echo "connect to local database"
# 查询数据
mysql -u root -p password db_name "select * from table_name"
# 更新数据
mysql -u root -p password db_name "update table_name set column1='value1' where condition"
# 删除数据
mysql -u root -p password db_name "delete from table_name where condition"
```
5. 定时任务:
```sh
#!/bin/sh
# 每小时执行一次任务
crontab -e
# 每分钟执行一次任务
crontab -e
# 每天执行一次任务
crontab -e
# 每周执行一次任务
crontab -e
```
这些只是 sh 脚本的一些基本用法,实际上 sh 脚本的功能非常强大,可以根据需要编写各种复杂的脚本来自动化各种任务。