天天看點

腳本結構和執行

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,如需轉載請自行聯系原作者