为什么 ./example 和 sh example 的行为不同? (`zsh:文本文件忙:./example`)

Why does behavior of `./example` and `sh example` different? (`zsh: text file busy: ./example`)

我试过这个:

$ exec 3> example
$ lsof example 
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
zsh     10711 lane    3w   REG  253,0        0 9231034 example

如果我这样做,它将显示 text file busy

$ ./example  
zsh: text file busy: ./example

但是如果我使用sh执行它就可以了(没有错误):

$ sh example 
$

./examplesh example有什么区别?

当你这样做时

$ ./example

您正在尝试 execute 正在写入的“示例”。这是不允许的。

当你这样做时

$ sh example

shreading“例子”,然后执行读取的内容。这很好。

Linux 上有不同的 shell 环境,例如 sh, bash, zsh and etc。 在某些shell中,bash脚本的执行存在差异。 当您使用 ./example 时,它会执行当前 shell 中的脚本。 当您与 bash examplesh example 一起使用时,它将 运行 在您想要的特定 shell 上。 您也可以在脚本的第一行使用 shebang