Which of the following are requirements in order to run a shell script like a regular command from anywhere in the filesystem? (Choose THREE correct answers.)



A. The user issuing the command must be in the group script.
B. The script file must be found in the $PATH.
C. The script file must have the executable permission bit set.
D. The script must begin with a shebang-line (#!) that points to the correct interpreter.
E. The file system on which the script resides must be mounted with the option scripts.

題解

選項A,只要有腳本檔案的執行權限就可以執行,不一定要在同樣的群組內。

選項B,「$PATH」環境變數所指向的目錄會成為shell在搜尋外部腳本檔案或二進制執行檔案的依據。為正確答案。

選項C,被執行的腳本檔案必須要擁有執行權限才可執行,否則只能透過「bash」、「sh」等指令的方使傳入腳本檔案的路徑作為參數才可以執行。為正確答案。

選項D,腳本檔案的第一行必須要使用「#!」明定出指令檔案要使用哪個腳本直譯器來開啟。為正確答案。如果是bash的腳本,第一行的寫法如下:

#!/bin/bash

選項E,執行腳本檔案和檔案系統的掛載參數並沒有什麼關聯。