当今最好的(可移植且可维护的)Shell 脚本语言是什么?
我知道这个问题在过去曾引发过“宗教”战争,可能没有一个正确的答案。 但是,在过去 3-4 年使用 ksh 和 csh 并经历了从一个版本移植到另一个版本或将通用逻辑片段应用于多个版本(作为遗留代码读取)的痛苦之后,如果我正在编写一个新脚本,我会选择 ksh,但出于强迫而不是选择。 除了 ksh/csh 之外还有更好的选择吗? 还有一些可以跨 UNIX (Solaris/HP/IBM/FreeBSD) 和 Linux 移植的东西(如果我没有要求太多,或者它确实对所有 Linux 风格都有意义)
等待建议......
和平:) 德旺卡姆达尔
I know this question has kind-a started "religious" wars in past and there might not be one correct answer. But after working with ksh and csh for last 3-4 years and going through the pain of porting from one to another or applying a common piece of logic to multiple versions (read as legacy code), if I am writing a new script, I would go for ksh, but out of compulsion rather than choice. Is there a better option other than ksh/csh? Also something that is portable across Unixes (Solaris/HP/IBM/FreeBSD) and Linux (and if I am not asking too much or it if does make sense all Linux flavors)
Waiting for suggestions ...
Peace :)
Devang Kamdar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我建议使用普通的旧
sh
,它随处可用。另外,值得注意的是,可移植性不仅涉及 shell,还涉及脚本中使用的其他命令,例如
awk
、grep
、ps
或 <代码>回显。I would suggest plain old
sh
, which is available everywhere.Also, it is worth noting that portability involves not only shell but also other commands used in a script such as
awk
,grep
,ps
orecho
.如果你真的希望它是可移植的(我不知道任何 shell 脚本都是可维护的),我会指定 #!/bin/sh 并使用 dash 以及如果可能的话其他 shell。
If you really want it to be portable (I don't know that any shell-script is maintainable), I would specify #!/bin/sh and test with dash and if possible other shells.
我希望 BASH 成为目前应用最广泛的 shell,因为它是许多 Linux 发行版的默认设置(它甚至可以通过 cygwin 在 Windows 上运行,但对于其他 shell 来说也可能如此)。
另一种选择可能是不使用 shell 本身来编写脚本,而是使用其中一种脚本语言,例如 perl、python、ruby...
I would expect BASH to be the widest spread shell at the moment since it is the default for many Linux distributions (it can even run on Windows with cygwin, but that's probably true for the other shells, too).
An alternative might be to not use the shell itself for scripting but one of the scriping languages out there like perl, python, ruby, ...
我一般用ksh。 我发现它在功能和便携性之间取得了很好的折衷。 大多数 Linux 机器和 Solaris 上都有它(或者有可用的兼容版本)。 我已经有一段时间没有使用 HP-UX 了(谢天谢地),但我很确定 HP-UX 也可用。
如果您需要支持的所有机器都是现代的,那么 bash 可能是一个选择。 Solaris 10 附带了一份副本。 这是大多数 Linux 机器上的默认设置。
您的最低公分母将是 Bourne (sh),因此如果可移植性是您主要关心的问题,那么这是值得考虑的。 但它缺少 ksh 和 bash 的一些更友好的功能。
仍然值得避开 csh/tcsh 进行脚本编写。 Csh 编程被认为是有害的 已经很老了,但仍然很重要。
I usually use ksh. I find that it's a good compromise between features and portability. It's there (or a compatible version is available) on most Linux boxes and Solaris. It's a while since I used HP-UX (thankfully) but I'm pretty sure it was available there too.
If all the machines you need to support are modern, bash might be an option. Solaris 10 comes with a copy. It's the default on most Linux machines.
Your lowest common denominator is going to be Bourne (sh), so that's worth considering if portability is your main concern. It's missing some of the more friendly features of ksh and bash though.
It's still worth steering clear of csh/tcsh for scripting. Csh Programming Considered Harmful is an oldie but still largely relevant.
我的答案是perl。
'sh' 'bash' 等可以以更好更优雅的方式做的一切。
而且它实际上更便携。 给定版本的 perl 在所有平台上都非常一致。 Linux、Solaris 和 AIX 发行版之间没有显着差异,但在这些平台之间移植 shell 脚本却非常痛苦。
它适用于所有 Windows 平台! 如果您避免反引号和“system()”,您的脚本就有很好的运行机会。
My answer would be perl.
Does everything 'sh' 'bash' etc can do in a nicer more elegant manner.
Also it is actually more portable. A given version perl is very consistant accross all platforms. There are no significant differences between the Linux, Solaris and AIX distributions whereas porting shell scripts between these platforms is a real pain.
And it works on all windows paltforms! Provided you avoid backticks and "system()" your script has a good chance of running.
Python! 查看 iPython,它是一个增强的 Python 解释器。 另外:Python 用于 Unix 和 Linux 系统管理。
您可以编写出色的可移植脚本,这很有趣。
Python! Check out iPython, which is an enhanced Python interpreter. Also: Python for Unix and Linux System Adminitration.
You can write great portable scripts, and it's fun.