您的位置:首页 > 运维架构 > Shell

20.2 shell脚本结构和执行

2018-09-11 20:50 537 查看

shell脚本结构和执行

开头需要加#!/bin/bash

以#开头的行作为解释说明

脚本的名字以.sh结尾,用于区分这是一个shell脚本

执行方法有两种

chmod +x 1.sh; ./1.sh

bash 1.sh

查看脚本执行过程 bash -x 1.sh

查看脚本是否语法错误 bash -n 1.sh

shell脚本结构和执行

1、创建一个shell文件夹,并切入到文件夹中

[root@xuexi-001 ~]# mkdir shell
[root@xuexi-001 ~]# cd shell/
[root@xuexi-001 shell]#

2、写shell脚本

#! /bin/bash //第一行必须这么写,固定格式,作用就是脚本若是在当台机器上去执行,可以不加这一行也没关系,因为它知道下面若干条的命令能在这台机器上去执行,去解析

[root@xuexi-001 shell]# vi 01.sh
#! /bin/bash   	//固定格式
echo "123"
w
ls
保存退出

3、执行脚本——>sh 01.sh

[root@xuexi-001 shell]# sh 01.sh
123
20:57:28 up 19 min,  2 users,  load average: 0.00, 0.01, 0.06
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      20:40   17:28   0.02s  0.02s -bash
root     pts/0    192.168.5.1      20:40    0.00s  0.01s  0.00s sh 01.sh
01.sh

4、在当前终端里,把01.sh中的#! /bin/bash 去掉后在执行脚本,会看到得到的结果相同,不会出现任何的问题,这就说明这台机器是能识别里面一条一条的命令的,去运行这里面的命令;但若是换一台机器,就不一定能执行了

5、在第一行,文件头指定 #!/bin/bash ,接下来要运行的命令是通过哪一个解释器来操作的,通常都是 /bin/bash 解释器来执行的

6、给01.sh文件一个执行权限

[root@xuexi-001 shell]# chmod a+x 01.sh

7、执行shell脚本 ./01.sh 能这样执行,说明这些命令被解析了,被/bin/bash认识了

[root@xuexi-001 shell]# ./01.sh
123
21:00:26 up 22 min,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      20:40   20:26   0.02s  0.02s -bash
root     pts/0    192.168.5.1      20:40    2.00s  0.02s  0.00s /bin/bash ./01
01.sh

8、/bin/bash也是一条命令, /bin/bash 和 /bin/sh 是同一个文件

[root@hanfeng shell]# ls -l /bin/bash
-rwxr-xr-x. 1 root root 960368 6月  10 2014 /bin/bash
[root@hanfeng shell]# ls -l /bin/sh
lrwxrwxrwx. 1 root root 4 10月 21 00:47 /bin/sh -> bash
[root@hanfeng shell]#

9、01.sh文件内容就是被/bin/bash所解析的

10、若没有 /bin/bash ,可以使用 /bin/bash 01.sh去执行

[root@xuexi-001 shell]# /bin/bash 01.sh
123
21:01:05 up 22 min,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      20:40   21:05   0.02s  0.02s -bash
root     pts/0    192.168.5.1      20:40    1.00s  0.02s  0.00s /bin/bash 01.s
01.sh

11、若是在shell脚本中在写入第二遍 #号开头的行, 就表示解释说明的作用

12、脚本的一般都是以.sh结尾,是为了区分这是一个shell脚本,否则需要打开这个文件,才知道是shell脚本

13、运行shell脚本有两种方法

一种是sh 01.sh运行shell脚本

另一种方法

先 chmod a+x 1.sh 给文件加一个执行权限

再 ./1.sh 去执行

这里的 ./ 就相当于一个相对路径,相对当前一个路径

也可以使用绝对路径去执行脚本 /root/shell/01.sh ,其实就是找到这个文件去执行

[root@xuexi-001 shell]# /root/shell/01.sh
123
21:01:38 up 23 min,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      20:40   21:38   0.02s  0.02s -bash
root     pts/0    192.168.5.1      20:40    2.00s  0.02s  0.00s /bin/bash /roo
01.sh

14、查看脚本执行过程 sh -x 01.sh 或者bash -x 01.sh

-x,就是显示脚本执行的过程

每一个加号,表示一个操作步骤

[root@xuexi-001 shell]# sh -x 01.sh
+ echo 123
123
+ w
21:02:07 up 23 min,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      20:40   22:07   0.02s  0.02s -bash
root     pts/0    192.168.5.1      20:40    7.00s  0.02s  0.00s w
+ ls
01.sh

15、查看脚本是否有错误 sh -n 01.sh

若是没有任何的输出,那么脚本则没有错误

sh -n 01.sh命令是检测是否存在语法错误

[root@xuexi-001 shell]# sh -n 01.sh
[root@xuexi-001 shell]#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Bash
相关文章推荐