Skip to content

Commit e87f169

Browse files
authored
Ch06: Add exec*p quirk (#69)
1 parent aea6d13 commit e87f169

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

docs/Ch06/index.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,18 @@ drwxr-xr-x 2 ustc ustc 4096 11月 17 20:45 模板/
380380

381381
例如,某个 `foo.sh` 首行为 `#!/bin/bash`,则执行 `./foo.sh` 就等于执行 `/bin/bash ./foo.sh`。
382382

383+
??? question "没有 shebang 的脚本呢?"
384+
385+
你可能会注意到,有些脚本并没有 shebang,但是仍然可以在命令行中正常执行。这是因为尽管 Linux 操作系统无法识别这个脚本的解释器(Linux 要求合法的脚本文件前两个字节必须是 `#!`,否则返回 `ENOEXEC`),应用程序仍然可以自行采用不同的处理方式,例如:
386+
387+
1. 如果你当前使用的 shell 是 Bash,它会尝试启动一个新的 Bash 进程来执行该脚本。
388+
2. 对于一些其他应用程序,如果它们使用的是 `execlp`, `execvp` 或 `execvpe` 等 C 语言库函数来运行其他程序的话,含有 `p` 后缀的 exec 系列函数会尝试调用 `/bin/sh` 来执行脚本。
389+
3. 如果以上情况都不符合,那么程序可能会直接输出错误信息,例如:
390+
391+
```text
392+
./foo.sh: Exec format error
393+
```
394+
383395
Bash 也支持在同一个行中安排多个命令:
384396

385397
| **分隔符** | **说明** |

0 commit comments

Comments
 (0)