首頁 > 運維 > linux運維 > 主體

linux怎麼執行shell腳本

王林
發布: 2020-03-11 16:35:19
原創
5168 人瀏覽過

linux怎麼執行shell腳本

Linux中shell腳本的執行通常有4種方式,分別為工作目錄執行,絕對路徑執行,sh執行,shell環境執行。

首先,請看我們的腳本內容

[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
登入後複製

(推薦教學:linux教學

1、工作目錄執行

工作目錄執行,指的是執行腳本時,先進入到腳本所在的目錄(此時,稱為工作目錄),然後使用./腳本方式執行

[tan@tan scripts]$ ./test.sh
-bash: ./test.sh: Permission denied
[tan@tan scripts]$ chmod 764 test.sh
[tan@tan scripts]$ ./test.sh
Hello Shell
登入後複製

報了權限錯誤,這裡需要賦權,使用chmod 764 test.sh 賦權後就可以正常執行了。

2、絕對路徑執行

在絕對路徑中執行,指的是直接從根目錄/到腳本目錄的絕對路徑

[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執行

sh執行,指的是用腳本對應的sh或bash來接著腳本執行

[tan@tan scripts]$ sh test.sh 
Hello Shell
[tan@tan scripts]$ bash test.sh 
Hello Shell
登入後複製

4、shell環境執行

shell環境執行,指的是目前的shell環境中執行,可以使用. 接腳本或source 接腳本

[tan@tan scripts]$ . test.sh 
Hello Shell
[tan@tan scripts]$ source test.sh 
Hello Shell
登入後複製

相關教學推薦:linux影片教學

以上是linux怎麼執行shell腳本的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!