?* 20231020?寫這篇博文斷斷續(xù)續(xù)花了好幾天,為了說明不同shell在執(zhí)行同一腳本文件時(shí)的差別,我分別在csdn提供線上Linux環(huán)境 (使用的shell是zsh)和自己的電腦上(使用的shell是bash)做測(cè)試。功夫不負(fù)有心人,在其中一些實(shí)例中可以體現(xiàn)出zsh和bash的對(duì)腳本文件支持的差別,收獲匪淺……
一、第一個(gè)shell腳本:hello world!
前面我們陸續(xù)介紹了與Linux shell編程有關(guān)的數(shù)據(jù)類型、數(shù)據(jù)運(yùn)算、流程控制語(yǔ)句等基礎(chǔ)知識(shí),今天我們正式開始寫shell腳本了。
我們學(xué)習(xí)某種編程語(yǔ)言,通常寫的第一個(gè)程序就是輸出hello world!,今天我們就編寫和運(yùn)行第一個(gè)shell腳本寫的hello world!,假定腳本文件名為hello.sh。
二、?錄入腳本文件hello.sh
腳本文件本質(zhì)上是一個(gè)文本文件,我們有很多種方法可以來創(chuàng)建它,比如可以用vi或vim之類的文本編輯器,這類編輯器,對(duì)于用過DOS下編輯器的用戶來說,上手起來比較容易,而對(duì)于只有windows使用經(jīng)歷的用戶而言,剛開始用起來未必順手。
所以我們這里直接在命令行來創(chuàng)建。
(一)使用?echo?命令 和?輸出重定向?來創(chuàng)建腳本文件hello.sh
具體命令如下:
1.在zsh中測(cè)試
# csdn @ edu in ~ [20:28:14]?
$ echo "#! /bin/bash" > hello.sh# csdn @ edu in ~ [20:31:32]?
$ echo 'echo "My first shell script file:Hello world!"' >> hello.sh# csdn @ edu in ~ [20:32:28]?
$ echo "# My first shell script file ends." >> hello.sh# csdn @ edu in ~ [20:32:53]?
$?
?2.在bash中測(cè)試
user?@?host?:?~?$?echo?'#!?/bin/bash'?>?hello.sh
user?@?host?:?~?$?echo?'echo?"My?first?shell?script?file:Hello?world"'?>>?hello.sh
user?@?host?:?~?$?echo?'#?My?first?shell?script?file?ends.'?>>?hello.sh
3.需要注意的地方
使用這種方法創(chuàng)建腳本文件時(shí),有一點(diǎn)不方便的地方是需要注意雙引號(hào)和單引號(hào)的使用,有時(shí)需要使用轉(zhuǎn)義符。
(二)使用cp命令 和?/dev/stdin?來創(chuàng)建腳本文件hello.sh
我們也可以使用命令 :
cp /dev/stdin hello.sh
來錄入hello.sh的內(nèi)容。/dev/stdin 是Linux 系統(tǒng)中一個(gè)特殊的符號(hào)鏈接文件,指向當(dāng)前進(jìn)程的標(biāo)準(zhǔn)輸入文件描述符,代表標(biāo)準(zhǔn)輸入,比如鍵盤。
在輸完所有shell腳本內(nèi)容后,我們按Ctrl+Z鍵來結(jié)束。
但是用這種方法創(chuàng)建的腳本文件在執(zhí)行時(shí)通常會(huì)遇到問題:
1.在zsh中測(cè)試
# csdn @ edu in ~ [23:11:12]?
$ cp /dev/stdin hello.sh
#! /bin/bash
echo "My first shell script file:Hello world!"
# My first shell script file ends.
^Z
[1] ?+ 148 suspended ?cp /dev/stdin hello.sh# csdn @ edu in ~ [23:12:26] C:20
# csdn @ edu in ~ [12:33:07] C:127
$ ./hello.sh
zsh: text file busy: ./hello.sh
2.在bash中測(cè)試?
?user?@?host?:?~?$?cp?/dev/stdin?hello.sh
#!?/bin/bash
echo?"My?first?shell?script?file:Hello?world!"
#?My?first?shell?script?file?ends.
^Z
[1]+??已停止???????????????cp?/dev/stdin?hello.sh
user?@?host?:?~?$?./hello.sh?
bash:?./hello.sh:?/bin/bash:?解釋器錯(cuò)誤:?文本文件忙
這是因?yàn)閏p命令執(zhí)行后,對(duì)應(yīng)的進(jìn)程并沒結(jié)束,hello.sh仍處于被cp進(jìn)程打開操作的狀態(tài)。
解決的方法我們以后會(huì)介紹。
二、查看腳本文件hello.sh的內(nèi)容
要查看腳本文件hello.sh的內(nèi)容,方法同樣有很多種。
(一)使用cat命令查看
我們可以使用命令
cat hello.sh
來查看剛才輸入的hello.sh 的內(nèi)容:
1.在zsh中測(cè)試
# csdn @ edu in ~ [23:12:26] C:20
$ cat hello.sh
#! /bin/bash
echo "My first shell script file:Hello world!"
# My first shell script file ends.# csdn @ edu in ~ [23:12:36]?
2.在bash中測(cè)試
user?@?host?:?~?$?cat?hello.sh
#!?/bin/bash
echo "My first shell script file:Hello world!"
#?My?first?shell?script?file?ends.
user?@?host?:?~?$?
?
?(二)使用cp命令 和 /dev/stdout來查看
?/dev/stdout 是Linux 系統(tǒng)中一個(gè)特殊的符號(hào)鏈接文件,指向當(dāng)前進(jìn)程的標(biāo)準(zhǔn)輸出文件描述符,代表標(biāo)準(zhǔn)輸出,比如顯示器屏幕。
1.在zsh中測(cè)試
# csdn @ edu in ~ [21:11:30]?
$ cp hello.sh /dev/stdout
#! /bin/bash
echo "My first shell script file:Hello world!"
# My first shell script file ends.# csdn @ edu in ~ [21:11:46]?
$?
2.在bash中測(cè)試
user?@?host?:?~?$?cp?hello.sh?/dev/stdout
#!?/bin/bash
echo?"My?first?shell?script?file:Hello?world"
#?My?first?shell?script?file?ends.
user?@?host?:?~?$?
三、shell腳本解說
在上面的hello.sh中,共有三行:
#! /bin/bash
echo "My first shell script file:Hello world!"
# My first shell script file ends.
我們逐行說明。
(一)第一行:shebang或hashbang語(yǔ)句:指定腳本解釋器
第一行是
#! /bin/bash
1.shebang或hashbang語(yǔ)句的作用
以#! 開頭,通常稱為shebang或hashbang,用于指定默認(rèn)情況下運(yùn)行指定腳本的解釋器路徑,在上面的實(shí)例中我們指定的解釋器是 /bin/bash。
這一行并不是必須的。如果一個(gè)腳本沒有添加 shebang 行來指定解釋器路徑,則默認(rèn)情況下系統(tǒng)會(huì)使用默認(rèn)的 shell 來執(zhí)行腳本。默認(rèn)shell可以使用echo $0 或?echo $SHELL?查看。
由于目前可以在Linux系統(tǒng)上運(yùn)行的shell有許多種:sh、bash、cshell、tcsh、zsh……盡管這些shell大多具有共同的語(yǔ)法,但它們確實(shí)有不同的語(yǔ)法或不同選項(xiàng)的處理方式,因此,同一個(gè)shell腳本文件在不同的shell中運(yùn)行時(shí)可能會(huì)產(chǎn)生不同的結(jié)果。為了確保腳本文件獲得預(yù)期的效果,我們建議為腳本指定解釋器。
常見的解釋器類型有:
#!/bin/sh
#!/bin/bash
#!/usr/bin/perl
#!/usr/bin/tcl
#!/bin/sed -f
#!/usr/awk -f
其中最常用、也是最常見的是?#!/bin/bash。
2.shebang或hashbang語(yǔ)句名稱的由來
在Unix行話中,用sharp或hash(有時(shí)是mesh)來稱呼字符#,用bang稱呼字符!,因而shebang或 hashbang合起來就代表了這兩個(gè)字符。到linux中也傳承了這一傳統(tǒng)。
(二)第二行:命令或語(yǔ)句
echo "My first shell script file:Hello world!"
這一行是echo命令,用來顯示字符串。在shell腳本文件中,除了第一行可能是shebang或hashbang語(yǔ)句外,接下來的語(yǔ)句可以是變是賦值命令,或者ls等命令,或者是if等流程控制語(yǔ)句。
(三)第三行:以#開頭的注釋
# My first shell script file ends.
在shell腳本文件中,除了第一行以#! 開頭的shebang或hashbang語(yǔ)句外,其他行中以#開始的內(nèi)容均被視為注釋。
關(guān)于注釋,有兩點(diǎn)說明:
1.注釋的兩種寫法
有兩種寫法。
一種寫法是像hello.sh的第3行這樣將注釋寫成一個(gè)獨(dú)立的行。
另一種寫法是將注釋可以直接寫在要注釋的命令之后。比如:
echo "My first shell script file:Hello world!" # dispaly the string
其中?# dispaly the string?就是我們添加的注釋。
2.一個(gè)腳本文件中可以有包含多個(gè)注釋。
我們可以根據(jù)需要,在腳本文件中添加許多個(gè)注釋,可以寫在命令之后,也可以單獨(dú)成行。
三、執(zhí)行腳本文件hello.sh
通常來說,執(zhí)行腳本文件有很多種方法 。我們先看看其中三種最常見的方法:
(一)shell解釋器名稱 腳本文件名
其實(shí)就是將腳本文件名作為命令參數(shù)傳遞給 shell解釋器執(zhí)行。
1.使用bash來執(zhí)行
由于我們?cè)趆ello.sh的shebang或hashbang語(yǔ)句中指定使用bash作為解釋器,所以我們可以用命令
bash hello.sh
?來執(zhí)行hello.sh
# csdn @ edu in ~ [23:12:36]?
$ bash hello.sh
My first shell script file:Hello world!# csdn @ edu in ~ [23:17:46]?
2.使用sh來執(zhí)行
我們也可以用命令
sh hello.sh
來執(zhí)行:
# csdn @ edu in ~ [23:17:46]?
$ sh hello.sh
My first shell script file:Hello world!# csdn @ edu in ~ [23:19:27]?
(二)腳本文件說明符
腳本文件說明符的格式是:
路徑/腳本文件名
其中路徑又分為絕對(duì)路徑和相對(duì)路徑。所以這種方式又可以細(xì)分為兩種格式。
1.相對(duì)路徑/腳本文件名
由于我們是在當(dāng)前目錄下創(chuàng)建了hello.sh,所以hello.sh的相對(duì)路徑就是./,我們使用命令
./hello.sh
來運(yùn)行看看。
(1)在zsh中測(cè)試
# csdn @ edu in ~ [12:27:14] C:127
$ ./hello.sh?
zsh: permission denied: ./hello.sh
看來是權(quán)限問題,我們使用ls -l命令查看?文件hello.sh的詳細(xì)信息:
# csdn @ edu in ~ [12:29:22] C:126
$ ls -l hello.sh ? ? ?
-rw------- 1 csdn csdn 95 10月 20 12:23 hello.sh
我們只有讀(r)寫(w)權(quán)限,還沒有執(zhí)行(x)權(quán)限,我們使用命令
chmod a+x hello.sh
增加執(zhí)行權(quán)限。
# csdn @ edu in ~ [12:31:53] C:1
$ chmod a+x ?hello.sh?# csdn @ edu in ~ [12:32:53]?
$ ls -l hello.sh
-rwx--x--x 1 csdn csdn 95 10月 20 12:23 hello.sh
?這下有執(zhí)行權(quán)限(x)了。再試試看:
# csdn @ edu in ~ [20:36:35]?
$ ./hello.sh ? ? ? ?
My first shell script file:Hello world!
?(2)在bash中測(cè)試
user?@?host?:?~?$?./hello.sh
bash:?./hello.sh:?權(quán)限不夠
user?@?host?:?~?$?ls?-l?hello.sh
-rw--w----?1?gxxc?gxxc?94?10月?20?18:36?hello.sh
user?@?host?:?~?$?chmod?+x?hello.sh
user?@?host?:?~?$?ls?-l?hello.sh
-rwx-wx--x?1?gxxc?gxxc?94?10月?20?18:36?hello.sh
user?@?host?:?~?$?./hello.sh
My?first?shell?script?file:Hello?world
user?@?host?:?~?$?
2.絕對(duì)路徑/腳本文件名
(1)在zsh中測(cè)試
我們先用pwd命令查看當(dāng)前目錄路徑:
# csdn @ edu in ~ [20:36:40]?
$ pwd
/home/csdn
?可以看到,當(dāng)前目錄路徑是/home/csdn,所以腳本文件的絕對(duì)路徑文件說明符是/home/csdn/hello.sh。
那么我們可以通過命令:/home/csdn/hello.sh 來執(zhí)行腳本:
# csdn @ edu in ~ [20:38:39]?
$ /home/csdn/hello.sh
My first shell script file:Hello world!
(2)在bash中測(cè)試
user?@?host?:?~?$?pwd
/tmp
user?@?host?:?~?$?/tmp/hello.sh
My?first?shell?script?file:Hello?world
user?@?host?:?~?$?
?在上面的例子中,當(dāng)前目錄的路徑是/tmp,所以腳本文件的絕對(duì)路徑文件說明符是/tmp/hello.sh。
所以我們可以通過命令:/tmp/hello.sh 來執(zhí)行腳本。
(3)需要的注意地方
不管使用相對(duì)路徑還是絕對(duì)路徑來執(zhí)行腳本文件,我們都要使用chmod?命令來為腳本文件增加執(zhí)行(x)權(quán)限。
(三)利用source命令來執(zhí)行。
有沒有不用修改腳本文件權(quán)限又能執(zhí)行腳本文件的方法呢?除了上面介紹的第一種方法:將腳本文件名作為命令參數(shù)傳遞給 shell解釋器執(zhí)行 外,我們還可以使用source命令來實(shí)現(xiàn)。
source 是 Shell 內(nèi)置命令的一種,它會(huì)忽略腳本文件的權(quán)限,讀取腳本文件,并依次執(zhí)行其中的命令語(yǔ)句。
使用source命令執(zhí)行腳本文件的格式是:
source?腳本文件說明符
也可以簡(jiǎn)寫為:
. 腳本文件說明符
?注意:簡(jiǎn)寫命令格式中.?和 腳本文件說明符之間要用空格分隔。
1.在zsh中測(cè)試
# csdn @ edu in ~ [20:38:50]?
$ source ./hello.sh
My first shell script file:Hello world!# csdn @ edu in ~ [20:47:56]?
$ . ./hello.sh
My first shell script file:Hello world!# csdn @ edu in ~ [20:48:25]?
$ source hello.sh ?
My first shell script file:Hello world!# csdn @ edu in ~ [20:49:07]?
$ . hello.sh
.: no such file or directory: hello.sh# csdn @ edu in ~ [20:49:15] C:127
可見,在zsh中,使用“source?腳本文件說明符”來執(zhí)行腳本文件時(shí),如果腳本文件在當(dāng)前目錄中,那么文件說明符中的路徑可以省略。
使用“.?腳本文件說明符” 這種簡(jiǎn)寫格式來執(zhí)行腳本文件時(shí),腳本文件說明符的路徑不能少。
2.在bash中測(cè)試
user?@?host?:?~?$?source?hello.sh
My?first?shell?script?file:Hello?world
user?@?host?:?~?$?source?./hello.sh
My?first?shell?script?file:Hello?world
user?@?host?:?~?$?.?hello.sh
My?first?shell?script?file:Hello?world
user?@?host?:?~?$?.?./hello.sh
My?first?shell?script?file:Hello?world
??
可見,在bash中,不管使用“source?腳本文件說明符”還是“.?腳本文件說明符” 這種簡(jiǎn)寫格式來執(zhí)行腳本文件時(shí),如果腳本文件在當(dāng)前目錄下,那么文件說明符中的路徑都可以省略。文章來源:http://www.zghlxwxcb.cn/news/detail-722590.html
對(duì)比可見bash用起來更方便一些,bash能成為最流行的shell,這是其中的原因之一。文章來源地址http://www.zghlxwxcb.cn/news/detail-722590.html
到了這里,關(guān)于Linux shell編程學(xué)習(xí)筆記14:編寫和運(yùn)行第一個(gè)shell腳本hello world!的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!