Linux Shell指令碼的執行方式

2021-07-11 23:58:18 字數 1343 閱讀 8088

[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 ~]# ./hello.sh

-bash: ./ hello.sh: 許可權不夠

2.以絕對路徑的方式執行:

[root@localhost ~]# /root/desktop/hello.sh

-bash: /root/desktop/ hello.sh: 許可權不夠

3.直接用bash或sh執行:

[root@localhost ~]# bash hello.sh

hello guys!

welcome to my blog

[root@localhost ~]# pwd

/root

[root@localhost ~]# sh hello.sh

hello guys!

welcome to my blog:linuxboy.org!

[root@localhost ~]# pwd

/root

以上三種方法執行shell指令碼,現行的shell會開啟乙個子shell環境,去執行shell指令碼,前兩種必須要有執行許可權才能夠執行

讓shell指令碼在現行的shell中執行:

4.現行的shell中執行

[root@localhost ~]# . hello.sh

hello guys!

welcome to my blog:linuxboy.org!

[root@localhost tmp]# pwd

/tmp

[root@localhost ~]# source hello.sh

hello guys!

welcome to my blog:linuxboy.org!

[root@localhost tmp]# pwd

/tmp

對於第4種不會建立子程序,而是在父程序中直接執行

子程序不能改變父程序的執行環境,所以cd(內建命令,只有內建命令才可以改變shell 的執行環境)沒有成功,但是第4種沒有子程序,所以cd成功

Linux shell指令碼的建立與執行

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

Linux shell指令碼執行方法總結

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的檔案屬性,如下所示 b...

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

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