将 Bash 脚本转换为 Shell
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33394706/
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
Convert Bash Scripts To Shell
提问by zuckermanori
I have a list of scripts written in Bash, and now i need to convert them to Shell scripts. I know bash is an extended implementation of the Shell specification, and so, i'm looking for some specification of the extension implemented in Bash, namely, what's in Bash that is not part of the Shell specification, so i could go over my scripts and make the appropriate modification. Is there such a document? Alternatively, any other suggestion on how to do the conversion will be greatly appreciated.
我有一个用 Bash 编写的脚本列表,现在我需要将它们转换为 Shell 脚本。我知道 bash 是 Shell 规范的扩展实现,因此,我正在寻找在 Bash 中实现的扩展的一些规范,即 Bash 中不属于 Shell 规范的内容,因此我可以查看我的脚本并进行适当的修改。有这样的文件吗?或者,将不胜感激有关如何进行转换的任何其他建议。
采纳答案by l0b0
To change Bash scripts to POSIX compatible shellcode you have to remove any bashisms. While I don't think there is any tool out there that can reliably handle allbashisms you can start with checkbashisms
.
要将 Bash 脚本更改为与POSIX 兼容的 shell代码,您必须删除任何bashisms。虽然我不认为有任何工具,在那里,可以可靠地处理所有你可以开始bash化checkbashisms
。
回答by Etan Reisner
The shellchecktool (with a shebang of #!/bin/sh
) will catch many bash-isms.
该shellcheck工具(用的家当#!/bin/sh
)将赶上很多的bash-主义。
Any that aren't caught should probably be filed as bugs against the project. If they aren't prohibitive to implement they should get added in reasonable time. The author is fairly responsive.
任何没有被发现的都应该作为针对该项目的错误提交。如果它们不禁止实施,则应在合理的时间内添加。作者相当有反应。
回答by Christophe Vu-Brugier
If you use a Debian based distribution, you can use the checkbashismscommand which is provided by the devscriptspackage.
如果您使用基于 Debian 的发行版,您可以使用devscripts包提供的checkbashisms命令。
Here is an example of a bash specific construct (a bashism) highlighed by checkbashisms: using ==
instead of =
in a test.
这是checkbashisms突出显示的 bash 特定构造(bashism)的示例: using==
而不是=
在测试中。
$ cat b.sh #!/bin/sh VAR = foo if [ $VAR == bar ]; 然后 回声“酒吧!” 菲
$ checkbashisms b.sh possible bashism in b.sh line 4 (should be 'b = a'): if [ $VAR == bar ]; then