Deb 包维护者脚本应该使用 Bash 还是 Sh

Should Deb package maintainer script use Bash or Sh

在 Deb 包维护者脚本中,例如postinstall、prerm 等。我想我们经常使用 /bin/sh 作为 shell 脚本解释器。但是,/bin/sh 可能因不同的发行版或用户偏好而有所不同。例如,Ubuntu link /bin/sh 到 /bin/dash,或者某些用途可能 link 到 /bin/tcsh。

如果用户将 shell 更改为 tcsh,我遇到的两种语法可能会失败。

1) set -e # tcsh 无法理解这个。

2) > /dev/null 2>&1 # 不明确的重定向。

第一个想法是去掉(1),把(2)改成>&/dev/null。但是,我发现在大多数维护者脚本中,都显示了 'set -e' 行。

现在,我不确定

a) 我只是解决所有问题并使用 /bin/sh b) 改为使用 /bin/bash c) 忽略 tcsh 大小写

谁能提供一些建议?

谢谢。 杰克

Debian policy doesn't force you to use /bin/sh as shell, you can use any other one provided you name it correctly (with the Shebang) 并且它在您尝试安装的系统上可用(如果您在预安装脚本中使用它,它会限制您使用默认提供的 shells)。

但是 Debian 政策告诉你 /bin/sh,不管它到底是什么,将始终执行 SUSv3 Shell 命令语言(即:一个 shell 即 POSIX 兼容)和一些附加功能。这就是您的维护者脚本可以依赖的。

在 Debian 上,/bin/sh/bin/bash,直到 Lenny。从 Squeeze 开始,现在是 /bin/dash。见 Debian wiki for more information.

在 Debian 或任何衍生产品上,如果 /bin/sh 实际上链接到 /bin/tcsh,它将是本地管理员更改的本地设置。这可能不仅会破坏您的脚本,还会破坏许多其他脚本。 Debian 政策文件实际上告诉维护者避免 cshtcsh 作为脚本语言。

总而言之,我认为您不必担心您的脚本与 tcsh 兼容,最佳做法是仅使用 /bin/sh 来满足您所有的打包需求。

如有疑问,请始终参考完整的 Debian policy document