Linux shell指令碼執行方法總結

2021-07-30 12:16:53 字數 2562 閱讀 5158

linux下新建乙個print hello world的指令碼程式,如下所示:

~/boke---> vim hello.sh

~/boke---> cat hello.sh

#!/bin/bash

#this is hello script

echo

'hello, i am yliu'

先來檢視hello.sh的檔案屬性,如下所示:

~/boke---> ll hello.sh

-rw-r--r-- 1 yliu sw 60 apr 13

16:16 hello.sh

可以發現hello.sh的檔案所有者、所有者所在組及其他使用者都沒有執行的許可權,即沒有x許可權

~/boke--

-> bash

hello.sh

hello,i

amyliu

#新增執行許可權,3類物件,2種方法

~/boke---> chmod u+x hello.sh #給所有者+執行許可權

~/boke---> ll hello.sh

-rwxr--r-- 1 yliu sw 60 apr 13

16:16 hello.sh

~/boke--->

~/boke---> chmod g+x hello.sh #給所有者所在組+執行許可權

~/boke---> ll hello.sh

-rwxr-xr-- 1 yliu sw 60 apr 13

16:16 hello.sh

~/boke--->

~/boke---> chmod o+x hello.sh #給其他人+執行許可權

~/boke---> ll hello.sh

-rwxr-xr-x 1 yliu sw 60 apr 13

16:16 hello.sh

~/boke--->

~/boke---> chmod a+x hello.sh #給所有人+執行許可權 等於前面3條命令的和

~/boke---> ll hello.sh

-rwxr-xr-x 1 yliu sw 60 apr 13

16:16 hello.sh

#一般使用最後一種方法即chmod a+x hello.sh來新增許可權即可

#新增許可權後執行該指令碼程式,指定指令碼路徑即可執行(**```#!/bin/bash```必須寫)

~/boke---> ./hello.sh

hello, i am yliu

~/boke---> chmod a-x hello.sh      #取消所有人的執行許可權

~/boke---> ll hello.sh

-rw-r--r-- 1 yliu sw 60 apr 13

16:16 hello.sh

~/boke--->

~/boke---> . hello.sh #使用.命令執行

hello, i am yliu

~/boke--->

~/boke---> echo

$path

#檢視系統環境變數$path目錄

/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin

~/boke---> chmod a+x hello.sh #新增x許可權

~/boke---> sudo cp hello.sh /usr/bin/ #拷貝指令碼至$path中的乙個

~/boke---> ll /usr/bin/he #確認拷貝成功

head heimdal-krb5-config hello.sh helpviewer hexdump

~/boke---> ll /usr/bin/hello.sh #檢查指令碼x許可權

-rwxr-xr-x 1 root root 60 apr 13

16:45 /usr/bin/hello.sh

~/boke--->

~/boke---> cd ~ #切換至任意目錄

~---> he #tab鍵匹配生效

head heimdal-krb5-config hello.sh help helpviewer hexdump

~---> hello.sh #正確執行該指令碼

hello, i am yliu

~--->

Linux Shell指令碼的執行方式

root localhost home cd root root localhost vim hello.sh bin bash cd tmp echo hello guys echo welcome to my blog 1.切換到shell指令碼所在的目錄,執行 root localhost h...

Linux shell指令碼的建立與執行

在進行linux測試時編寫指令碼是必不可少的。最近經常使用linux,感覺太頻繁地敲擊鍵盤有些累了,於是想到了shell指令碼。可以把太多的命令寫成乙個指令碼,這樣每次執行一遍shell檔案,就可以省去了敲擊鍵盤的時間。於是在網上搜了一些有關linux下指令碼程式設計的內容。shell不僅僅是命令的...

Linux Shell指令碼的6中執行方式

建立乙個shell指令碼,方便說明 touch tmp test.sh chmod 755 tmp test.sh ll tmp test.sh.代表當前路徑,的意思就是在當前路徑下執行test.sh。如果不加.bash就會去path環境變數裡查詢,若查詢不到,會報錯找不到命令。cd tmp tes...