本文探討了在 Linux 上運行 shell 命令的各種方法。

在Linux 上工作時,您可能會遇到一些未按您預期的方式執行的文件——例如,您可能在當前目錄中有一個文件,但是當您輸入它的名稱時它沒有運行。你得到file_name.sh command not found了,但實際上,文件在那里。該文件不起作用,即使具有執行權限,因為當你在 shell 上寫一些東西并運行它時,你的$PATH變量會被檢查。如果目錄內有任何匹配的命令$PATH,例如/usr/bin,它將執行。除非有匹配的命令,否則您將收到錯誤消息。
因此,您需要解決文件的路徑。讓我們創建一個簡單的Linux shell 腳本并對該腳本具有執行權限。以下示例是用Bash Shell編寫的。
mkdir test && cd test
echo 'echo "hello world PID:$$ ParentPID:$PPID"' > test.sh
chmod 755 test.sh
創建腳本后,讓我們按順序執行它們。
殼1測試.sh2#test.sh:找不到命令34. 測試.sh5#hello world PID:19245 ParentPID:1924367. ./test.sh # 同上。它只是明確指定當前目錄。8#hello world PID:19245
ParentPID:19243910./test.sh #hello world PID:23044 ParentPID:1924511bash test.sh #hello world PID:23045 ParentPID:19245
$PATH除非當前目錄在環境變量中,否則直接給出文件名不起作用。當您使用POSIX標準 shell(如 ksh)時,第二個命令也會失敗,因為在運行命令 shell 時檢查/命令內部是否存在。如果有,那么它會查找您處理的當前工作目錄或絕對路徑。與此相反,然后它查看里面的命令$PATH。我當前正在處理的目錄不在 PATH 內,因此會出現錯誤。
test.sh
#test.sh: command not found
. test.sh
#hello world PID:19245 ParentPID:19243
. ./test.sh # this is same with above. it is just specifies current directory explicitly.
#hello world PID:19245 ParentPID:19243
./test.sh #hello world PID:23044 ParentPID:19245
bash test.sh #hello world PID:23045 ParentPID:19245
假設您正在使用Bash。通過執行兩者. file.sh或. ./file.sh結果來運行文件將是相同的,但不是./test.sh. source和bash命令呢?
如果您使用任何 shell 命令,例如 Bash 或 ksh,您將生成一個新的 shell 來運行該命令。因此,您設置的每個變量在新 shell 中都不可用。另一方面,source使用當前的 shell 并且不產生新的 shell 進程。因此,您在文件中所做的任何更改都會影響您當前的 shell。上面,正如您從輸出中看到的那樣,當您執行./或bash因為它們正在產生新進程時,PID 會發生變化。如上所示,命令的父進程 ID (PPID)bash test.sh等于命令的進程 ID (PID) . ./test.sh。
讓我們設置一個變量并在test.sh腳本中打印它。
bash -posix
test.sh
#bash: test.sh: command not found
. test.sh
#bash: .: test.sh: file not found
. ./test.sh
#hello world PID:23493 ParentPID:19245
./test.sh
#hello world PID:23539 ParentPID:23493
bash test.sh
#hello world PID:23540 ParentPID:23493
exit
顯然,該bash test.sh命令沒有給出$STR變量輸出,因為新的 shell 不知道。它沒有設置在新外殼中。讓我們在腳本中設置一個變量。
echo 'NEWSTR="WORLD"' >> test.sh
echo 'echo "NEWSTR is $NEWSTR"' >> test.sh
bash test.sh
#hello world PID:25318 ParentPID:19245
#STR is #NEWSTR is WORLD
echo $NEWSTR #this will give empty output
#
. test.sh
#hello world PID:19245 ParentPID:19243
#STR is HELLO #NEWSTR is WORLD
echo $NEWSTR
# WORLD
source test.sh
#hello world PID:19245 ParentPID:19243
# STR is # NEWSTR is WORLD
echo $NEWSTR
# WORLD
.并source在當前 shell 中運行,因此我們可以看到新變量。這就是運行該bash .bashrc命令不會更新您的 PATH 變量的原因。您應該使用source命令運行或使用.. 因此,您必須使用 source 命令來更改 PATH 變量。
最后,讓我們嘗試使用此信息來更改和設置 PATH 變量。
mkdir directory && cd directory
echo 'echo "FILE"' > file.sh && chmod 755 file.sh
echo 'echo "COMMAND"' > echocommand && chmod 755 echocommand
pwd
# /home/ofk/test/directory
cd
# change PATH variable inside your .profile (or where ever you set PATH) file and add above path
# PATH="$PATH:/home/ofk/test/directory"
bash .profile
# try to run echocommand or file.sh
echocommand
# echocommand: command not found
file.sh
# file.sh: command not found
source .profile
echocommand
# COMMAND
file.sh
# FILE
結論
./或shell命令(bash、ksh)啟動新的 shell 并運行命令。
. file_name或source命令在當前 shell 上運行。