您好,登錄后才能下訂單哦!
這篇文章主要為大家詳細介紹了linux執(zhí)行shell腳本的四種方式,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
Linux中shell腳本的執(zhí)行通常有4種方式,分別為工作目錄執(zhí)行,絕對路徑執(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í)行腳本時,先進入到腳本所在的目錄(此時,稱為工作目錄),然后使用 ./腳本方式執(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
報了權(quán)限錯誤,這里需要賦權(quán),使用chmod 764 test.sh 賦權(quán)后就可以正常執(zhí)行了。
2、絕對路徑執(zhí)行
絕對路徑中執(zhí)行,指的是直接從根目錄/到腳本目錄的絕對路徑
[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í)行,指的是用腳本對應的sh或bash來接著腳本執(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í)行,指的是在當前的shell環(huán)境中執(zhí)行,可以使用 . 接腳本 或 source 接腳本
[tan@tan scripts]$ . test.sh Hello Shell [tan@tan scripts]$ source test.sh Hello Shell
以上就是linux執(zhí)行shell腳本的四種方式的詳細內(nèi)容了,看完之后是否有所收獲呢?如果想了解更多相關內(nèi)容,歡迎來億速云行業(yè)資訊!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。