一、 shell脚本的基本写法
1)脚本第一行,魔法字符#!指定解释器 【必写】
#!/bin/bash 表示以下内容使用bash解释器解析
注意: 如果直接将解释器路径写死在脚本里,可能在某些系统就会存在找不到解释器的兼容性问题,所以可以使用: #!/bin/env 解释器
2)脚本第二部分,注释(#号)说明,对脚本的基本信息进行描述 【可选】
#!/bin/env bash
# 以下内容是对脚本的基本信息的描述
# Name: 名 字
# Desc:描述describe
# Path:存放路径
# Usage:用法
# Update:更新时间
#下面就是脚本的具体内容commands
...
3)脚本第三部分,脚本要实现的具体代码内容
二、shell脚本的执行方法
- 标准脚本执行方法(建议)
1) 编写人生第一个shell脚本
[root@MissHou shell01]# cat first_shell.sh
#!/bin/env bash
# 以下内容是对脚本的基本信息的描述
# Name: first_shell.sh
# Desc: num1
# Path: /shell01/first_shell.sh
# Usage:/shell01/first_shell.sh
# Update:2021-08-03
echo "hello world"
echo "hello world"
echo "hello world"
2)脚本增加可执行权限
[root@MissHou shell01]# chmod +x first_shell.sh
3)标准方式执行脚本
[root@MissHou shell01]# pwd
/shell01
[root@MissHou shell01]# /shell01/first_shell.sh
或者
[root@MissHou shell01]# ./first_shell.sh
注意:标准执行方式脚本必须要有可执行权限。