Bash 还是 Bourne 脚本?
编写 Bash 脚本还是 Bourne 脚本更好?我的团队编写了 Bourne 脚本,但我不完全确定为什么。
如果这是一个圣战问题(即:vim vs. emacs),请回复:圣战。
Is it better practice to write Bash scripts or Bourne scripts? My team writes Bourne scripts but I am not entirely sure why.
If this is a holy war question (ie: vim vs. emacs) please just reply: holy war.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这取决于您的目标平台是什么。
如果您只针对主要的 Linux 发行版和 Mac OS X,那么您可以确信这些系统将提供 bash。在其他 UNIX(例如,AIX、Solaris、HP-UX)上,bash 可能不一定存在,因此 Bourne 是更安全的选择。如果 bash 可用,我想不出您会更喜欢 Bourne 的任何理由。
It depends on what your target platform is.
If you're only targeting, say, major Linux distributions and Mac OS X, then you can be confident that these systems will have bash available. On other UNIXes (e.g., AIX, Solaris, HP-UX), bash may not necessarily be present, so Bourne is the safer choice. If bash is available, I can think of no reason you might prefer Bourne.
您可以更加确定 Bourne shell 将安装在任何给定的 Unix 计算机上。是的,Bash 在 Linux 上无处不在,但并不是整个世界都是 Linux。
You can be more sure that Bourne shell will be installed on any given Unix computer. Yeah, Bash is ubiquitous on Linux, but the whole world isn't Linux.
最重要的是要记住,并非每个操作系统都像某些 Linux 发行版那样将 /bin/sh 软链接到 /bin/bash。许多脚本是为 bash 编写的,但以:开头,
因此它们在 Ubuntu 中会崩溃。因此,当您编写 bash 脚本时,请始终编写:
The most important thing is to remember that not every OS softlinks /bin/sh to /bin/bash, as some Linux distros do. A lot of scripts are written for bash but begin with:
so that they break e.g. in Ubuntu. So, when you write bash script, always write:
嗯,这是一个品味问题,但对于初学者来说,bourne shell 脚本可以用 bash 运行,而且我认为 bash 具有 Bourne 无法运行的功能。
Well, is a matter of taste, but for starters, bourne shell scripts can be run with bash, and I think bash has features that cannot be run by Bourne.
我使用 Bash 作为登录 shell,但对于脚本编写,我会在一周中的任何一天选择 Bourne shell,并在周日选择两次。 Bash 具有更好的功能、更好的用户友好性和更好的错误。
实际上,让我在登录时选择 Bash 的同样因素,也让我在编写脚本时避免使用它。 Bash 试图让一切都对用户来说变得美好和舒适,但代价是 776 kB 的可执行文件(在我的机器上),而 Bourne shell 则为 140 kB。为什么我的脚本要关心用户友好性?我通过使用一些巧妙的 Bash 函数可能获得的任何收益都被 shell 占用空间有效地抵消了,shell 占用空间是原来的五倍多。
我有运行 Linux、FreeBSD 和 OS X 的计算机。虽然我很少在计算机之间移动任何东西,但有这种可能性真是太好了。在 Bourne shell 脚本中,您只需键入
即可运行。总是。 Bash 在 Linux 上可能很常见,但它不像 Bourne shell 那样标准化。在 FreeBSD 上,默认情况下不安装 Bash。如果系统管理员认为这是个好主意,则可以从 Ports 安装它,但即使如此,它最终也会出现在 /usr/local/bin/bash 中(而不是 /bin/bash >)。因此,如果您仍然决定使用 Bash,则应该编写脚本
以使脚本可移植。
env
将为您找到 shell,无论您的 Unix 风格如何(只要安装了它)。归根结底,这是您的选择。只需确保您的脚本实际上兼容您选择的 shell,而不是依赖于“sh”符号链接到“bash”或类似的东西。
I use Bash as my login shell, but for scripting I'd choose the Bourne shell any day of the week and twice on Sunday. Bash has better features, better user friendliness and better bugs.
Actually, the same stuff that makes me choose Bash when I'm logging in, makes me avoid it when scripting. Bash tries to make everything nice and cozy for the user, but at the expense of a 776 kB executable (on my machine), compared to 140 kB for Bourne shell. Why would my script care about user friendliness? Any gain I might achieve through the use of some clever Bash function is effectively cancelled out by the shell footprint, which is more than five times as big.
I have computers running Linux, FreeBSD and OS X. Although I rarely move anything between the computers, it's nice to have the possibility. In a Bourne shell script, you simply type
and it just works. Always. Bash might be common on Linux, but it's not as standardized as the Bourne shell. On FreeBSD, Bash is not installed by default. It can be installed from Ports if the sysadmin thinks it's a good idea but, even then, it ends up in /usr/local/bin/bash (not /bin/bash). Thus, if you still decide to go with Bash, you should write
to make the script portable.
env
will find the shell for you, regardless of your Unix flavor (as long as it's installed).At the end of the day, it's your choice. Just make sure that your scripts are actually compliant to the shell you choose, and not relying on "sh" being symlinked to "bash" or something similar.
可移植性。我会写
#!/bin/sh
除非事情变得非常痛苦,然后我会写#!/bin/bash
。世界正在迅速变化,我敢打赌,将来说服系统管理员安装 bash 会很容易。但我通过使用 Bourne 来对冲我的赌注,这很简单。Portability. I write
#!/bin/sh
unless things get really to painful, and then I write#!/bin/bash
. The world is changing very rapidly, and I'm betting that in the future it will be easy to convince sysadmins to install bash. But I hedge my bets by using Bourne for most stuff, which is simple.在 Mac OS X 上 /bin/sh 不是 Bourne shell。 (但是你可能会在新鲜的肉上得到真正的伯恩斯)。
要识别传统的 Bourne shell,您可以尝试使用抑扬符 ^(插入符号)来替代 | (管道)。
请参阅:
传统的 Bourne Shell 系列,
http://www.in-ulm.de/ ~mascheck/伯恩/
On Mac OS X /bin/sh is NOT a Bourne shell. (But you may get a true bournesh over at freshmeat).
To identify a traditional Bourne shell you may try to use the circumflex ^ (caret) as a replacement for | (pipe).
See:
The Traditional Bourne Shell Family,
http://www.in-ulm.de/~mascheck/bourne/
我会再次选择 bourne shell,因为 bourne shell 在 UNIX 实现之间可能略有不同。使用
bash
,您可以确保bash
始终是bash
。I'd go for bourne again shell, as the bourne shell can be slightly different among unix implementations. With
bash
you can be sure thatbash
is alwaysbash
.