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,运行脚本文件和文件系统的挂载参数并没有什么关联。