bash 为什么 /bin/sh 在 Mac 和 Ubuntu 上表现不同
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19943767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does /bin/sh behave differently on Mac and Ubuntu
提问by Jeffrey
I'm writing a sh file to get the modification time of a file. I want the sh file works on both Mac the Ubuntu.
我正在写一个 sh 文件来获取文件的修改时间。我希望 sh 文件适用于 Mac 和 Ubuntu。
I use the /bin/sh and add #!/bin/sh to the first line of the bash file. I suppose /bin/sh should behave the same on the two OS. But it doesn't. Below are the two examples for the differences.
我使用 /bin/sh 并将 #!/bin/sh 添加到 bash 文件的第一行。我想 /bin/sh 在两个操作系统上的行为应该是一样的。但事实并非如此。以下是差异的两个示例。
The script below works on Mac but not on Ubuntu.
modTime=$(stat -f "%m" -t "%s" $filepath)
And the script below works on Ubuntu but not on Mac.
modTime=$(date +%s -r $filepath)
下面的脚本适用于 Mac,但不适用于 Ubuntu。
modTime=$(stat -f "%m" -t "%s" $filepath)
下面的脚本适用于 Ubuntu,但不适用于 Mac。
modTime=$(date +%s -r $filepath)
My questions are:
我的问题是:
- Why the /bin/sh behaves differently on on Mac and Ubuntu?
- If I want to write the cross-platform sh script, how should I avoid the platform-dependent code?
- 为什么 /bin/sh 在 Mac 和 Ubuntu 上的行为不同?
- 如果我想写跨平台的sh脚本,应该如何避免平台相关的代码?
回答by thom
- Why the /bin/sh behaves differently on on Mac and Ubuntu?
- 为什么 /bin/sh 在 Mac 和 Ubuntu 上的行为不同?
because they are both different shells. sh on ubuntu is the dash shell, sh on minix is the ash shell, sh on slackware is the bash shell. And since not too long is sh on OSX also the bash shell
因为它们都是不同的壳。ubuntu 上的 sh 是 dash shell,minix 上的 sh 是 ash shell,slackware 上的 sh 是 bash shell。并且因为 OSX 上的 sh 时间不长,所以 bash shell
If you want same behaviour, specify your shell... #!/bin/sh is, although strictly the never-in-free-systems-implemented-bourne-shell, generally the system shell which can vary from system to system ...use #!/bin/bash to literally say that you mean bash, use #!/bin/ksh to literally call the korn-shell etc.
如果您想要相同的行为,请指定您的 shell...#!/bin/sh 是,尽管严格来说是 never-in-free-systems-implemented-bourne-shell,但通常系统 shell 可能因系统而异.. .use #!/bin/bash 从字面上说你的意思是 bash,使用 #!/bin/ksh 从字面上调用 korn-shell 等。
- If I want to write the cross-platforma sh script, how should I avoid the platform-dependent code?
- 如果我想写跨平台的a sh 脚本,我应该如何避免依赖平台的代码?
officially: keep strict to the POSIX rules to run on any POSIX compliant shell
pragmatically: write specific for one type of shell that is available on almost any system (not counting microcontrollers and SoC's or big irons and sparc stations, bash is available for most systems, but yes there are system who don't do bash so bash is not the best choice for 100% portability, but it is absolutely the most (ab)used ;-)
best portable: As an Ubuntu user rule of thumb: if it runs on dash (the /bin/sh of ubuntu) it will run on virtually anything (including your router , your toaster and your coffeemachine).
Above all: user2719058is right, OSX is not Linux but BSD-UNIX so while they can run the same shell, the commands are just different enough to make it very very difficult to write a one-script-fits-all. Choosing the best shell will not change that....so the value of a unified scripting language is hereby proven to be very limited unless the system commands on every system are POSIX compliant as well.
正式:严格遵守 POSIX 规则以在任何符合 POSIX 的 shell 上运行
务实:为几乎任何系统上都可用的一种类型的外壳编写特定的代码(不包括微控制器和 SoC 或大铁杆和 sparc 站,bash 适用于大多数系统,但是是的,有些系统不执行 bash,所以 bash 是不是 100% 可移植性的最佳选择,但它绝对是最(ab)使用的 ;-)
最佳便携性:作为 Ubuntu 用户的经验法则:如果它在 dash(ubuntu 的 /bin/sh)上运行,它几乎可以在任何东西上运行(包括您的路由器、烤面包机和咖啡机)。
最重要的是:user2719058是对的,OSX 不是 Linux,而是 BSD-UNIX,所以虽然它们可以运行相同的 shell,但命令的不同足以使编写一个脚本适合所有的脚本变得非常困难。选择最好的 shell 不会改变这一点……所以统一脚本语言的价值在此被证明是非常有限的,除非每个系统上的系统命令也符合 POSIX。
tl;dr:
Unified cross-platform scripting is a pipe dream because differences in binaries across systems prevent this.
tl; dr:
统一的跨平台脚本是一个白日梦,因为跨系统的二进制文件的差异阻止了这一点。
回答by user2719058
This is not the shell behaving differently, but the commands you are running. On Ubuntu, these come from the GNU world, on MacOS, they are probably some BSD variant. Which usually means, on MacOS you have fewer features. :)
这不是 shell 的行为不同,而是您正在运行的命令。在 Ubuntu 上,这些来自 GNU 世界,在 MacOS 上,它们可能是一些 BSD 变体。这通常意味着,在 MacOS 上,您的功能较少。:)
It should be noted that this (I guess) applies to /bin/sh
as well, but that's not the reason for the breakage you see.
应该注意的是,这(我猜)也适用于/bin/sh
,但这不是您看到的破损的原因。