天天看点

脚本结构和执行

1.开头需要加#!/bin/bash

[root@localhost ~]# mkdir shell

[root@localhost ~]# cd shell/

[root@localhost shell]# vi 1.sh

#!/bin/bash(本机可以不用这一行)表示使用这个解释器运行的

echo "123"

w

ls

[root@localhost shell]# . 01.sh //执行的方式有source、sh(因为有了!#/bin/bash)

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

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

[root@localhost shell]# ls

01.sh

4.执行方法有两种

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

[root@localhost shell]# chmod u+x 01.sh 

[root@localhost shell]# ./01.sh 

123

03:08:04 up 5:34, 1 user, load average: 0.00, 0.02, 0.05

USER TTY LOGIN@ IDLE JCPU PCPU WHAT

root pts/0 02:43 4.00s 1.00s 0.00s w

4.2 bash 1.sh

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

[root@localhost shell]# sh -x 01.sh //查看脚本执行过程

echo 123

03:18:23 up 5:45, 1 user, load average: 0.00, 0.04, 0.05

root pts/0 02:43 7.00s 1.17s 0.02s w

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

本文转自 虾米的春天 51CTO博客,原文链接:http://blog.51cto.com/lsxme/2056741,如需转载请自行联系原作者