這篇文章主要為大家詳細(xì)介紹了linux執(zhí)行shell腳本的四種方式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
Linux中shell腳本的執(zhí)行通常有4種方式,分別為工作目錄執(zhí)行,絕對(duì)路徑執(zhí)行,sh執(zhí)行,shell環(huán)境執(zhí)行。
首先,看下我們的腳本內(nèi)容
[tan@tan scripts]$ ll total 4 -rw-rw-r--. 1 tan tan 68 May 8 23:18 test.sh [tan@tan scripts]$ cat test.sh #!/usr/bin/bash /usr/bin/python <<-EOF print "Hello Shell" EOF
1、工作目錄執(zhí)行
工作目錄執(zhí)行,指的是執(zhí)行腳本時(shí),先進(jìn)入到腳本所在的目錄(此時(shí),稱為工作目錄),然后使用 ./腳本方式執(zhí)行
[tan@tan scripts]$ ./test.sh -bash: ./test.sh: Permission denied [tan@tan scripts]$ chmod 764 test.sh [tan@tan scripts]$ ./test.sh Hello Shell
報(bào)了權(quán)限錯(cuò)誤,這里需要賦權(quán),使用chmod 764 test.sh 賦權(quán)后就可以正常執(zhí)行了。
2、絕對(duì)路徑執(zhí)行
絕對(duì)路徑中執(zhí)行,指的是直接從根目錄/到腳本目錄的絕對(duì)路徑
[tan@tan scripts]$ pwd /home/tan/scripts [tan@tan scripts]$ `pwd`/test.sh Hello Shell [tan@tan scripts]$ /home/tan/scripts/test.sh Hello Shell
3、sh執(zhí)行
sh執(zhí)行,指的是用腳本對(duì)應(yīng)的sh或bash來(lái)接著腳本執(zhí)行
[tan@tan scripts]$ sh test.sh Hello Shell [tan@tan scripts]$ bash test.sh Hello Shell
4、shell環(huán)境執(zhí)行
shell環(huán)境執(zhí)行,指的是在當(dāng)前的shell環(huán)境中執(zhí)行,可以使用 . 接腳本 或 source 接腳本
[tan@tan scripts]$ . test.sh Hello Shell [tan@tan scripts]$ source test.sh Hello Shell
以上就是linux執(zhí)行shell腳本的四種方式的詳細(xì)內(nèi)容了,看完之后是否有所收獲呢?如果想了解更多相關(guān)內(nèi)容,歡迎來(lái)創(chuàng)新互聯(lián)行業(yè)資訊!