bash vs csh vs 其他 - 哪个更适合应用程序维护?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4317247/
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
bash vs csh vs others - which is better for application maintenance?
提问by LoudNPossiblyWrong
Possible Duplicate:
What Linux shell should I use?
可能的重复:
我应该使用什么 Linux shell?
I am starting to get proficient in a Linux environment and i'm trying to pick a weapon of choice in terms of command shell scripting (as i'm still a big n00b at this) that will help me (and others) manage, test and administer a set of server side applications running on a *NIX environment.
我开始精通 Linux 环境,我正试图在命令 shell 脚本方面选择一种武器(因为我在这方面仍然是一个大n00b),这将帮助我(和其他人)管理、测试并管理一组在 *NIX 环境中运行的服务器端应用程序。
My question is: What is(are) the preferred command shell(s) out there when the following criteria are considered:
我的问题是:当考虑以下条件时,首选命令 shell 是什么:
How easy is it to learn/understand for a junior dev who has never had an exposure to shell scripting?
Is there a big pool of developers out there that know this shell script?
Is it safe and easy to use - will script errors be silent or give intelligent error output, will it let the uninitiated shoot them selves in the foot?
How portable is it? - Can i expect the same script to run in OpenSolaris as well as Redhat, FreeBSD? (granted command syntax and options for specific OS will change accordingly)
How standard is it? Is it expected to be included on most distro's of *NIX or does it have to be installed additionally?
对于从未接触过 shell 脚本的初级开发人员来说,学习/理解有多容易?
是否有大量的开发人员知道这个 shell 脚本?
它是否安全且易于使用 - 脚本错误是否会保持沉默或提供智能错误输出,是否会让外行人自行射击?
它有多便携?- 我可以期望在 OpenSolaris 以及 Redhat、FreeBSD 中运行相同的脚本吗?(特定操作系统的授予命令语法和选项将相应更改)
它有多标准?预计它会包含在 *NIX 的大多数发行版中还是必须另外安装?
I understand that there are camps out there who hold strong feelings for/against specific command shells, i am just looking for an informed opinion.
我知道有些阵营对/反对特定的命令外壳有强烈的感情,我只是在寻找知情的意见。
采纳答案by Gilles 'SO- stop being evil'
These days, just about any non-embedded (or large embedded) operating system has a POSIX:2001 a.k.a. Single Unix v3compatibility layer. This is native on unix platforms (Linux, Mac?OS?X, Solaris, *BSD, etc.) and installable on other platforms such as Windows and Android. POSIX specifies a shelllanguage, usually known as POSIX sh. This language is derived from the Bourne shell.
如今,几乎所有非嵌入式(或大型嵌入式)操作系统都有POSIX:2001 aka Single Unix v3兼容层。这是在 unix 平台(Linux、Mac?OS?X、Solaris、*BSD 等)上原生的,也可以安装在其他平台上,例如 Windows 和 Android。POSIX 指定了一种shell语言,通常称为 POSIX sh。这种语言源自 Bourne shell。
Most unix systems have one of two implementations of POSIX sh: ksh or bash, which have additional useful features compared to POSIX. However some less mainstream systems (especially embedded ones) may have only POSIX-mandated features.
大多数 unix 系统具有 POSIX sh 的两种实现之一:ksh 或 bash,与 POSIX 相比,它们具有额外的有用功能。然而,一些不太主流的系统(尤其是嵌入式系统)可能只有 POSIX 规定的功能。
Given your objectives, I see three choices:
鉴于您的目标,我看到三个选择:
- Restrict yourself to POSIX sh. Pro: you don't have to worry about differing variants, since there's a standard and compliant implementations are readily available. Con: you don't benefit from bash and ksh's extensions.
- Use the intersection of ksh and bash. This is attractive in appearance, but it does mean you have to use two reference documents instead of just one — and even the features that bash and ksh have in common don't always use the same syntax. Figuring out which one you want to use on a given system is also a pain.
- Choose one of ksh or bash. Both bash and ksh are available on all unix-like platforms and on Windows. Both have an open source implementation (the only one for bash, ATT ksh93 for ksh) that can be installed on most platforms. I'd go for bash over ksh for two reasons. First, it's the default on Linux, so you'll find more people who're used to it. Second, there are systems that come with an older, less-featured implementation of ksh; even if you can install ksh93, it's another thing you have to think about when deploying.
- 限制自己使用 POSIX sh。优点:您不必担心不同的变体,因为有一个标准且合规的实现是现成的。缺点:您不会从 bash 和 ksh 的扩展中受益。
- 使用 ksh 和 bash 的交集。这在外观上很吸引人,但它确实意味着您必须使用两个参考文档而不是一个 — 甚至 bash 和 ksh 的共同功能也不总是使用相同的语法。弄清楚您想在给定系统上使用哪个也是一种痛苦。
- 选择 ksh 或 bash 之一。bash 和 ksh 在所有类 Unix 平台和 Windows 上都可用。两者都有一个可以安装在大多数平台上的开源实现(bash 的唯一实现,ksh 的 ATT ksh93)。出于两个原因,我会选择 bash 而不是 ksh。首先,它是 Linux 上的默认设置,因此您会发现更多人习惯了它。其次,有些系统带有较旧的、功能较少的 ksh 实现;即使可以安装ksh93,也是部署时要考虑的另一件事。
Forget about csh for scripting, and forget about zsh if you want common default availability.
如果您想要通用的默认可用性,请忘记用于脚本编写的 csh,并忘记 zsh。
See also What are the fundamental differences between the mainstream *NIX shells?, particularly the “for scripting” part of my answer.
另请参阅主流 *NIX shell 之间的根本区别是什么?,尤其是我的答案中的“脚本”部分。
Note that shell programming involves other utilities beyond the shell. POSIX specifies those other utilities. “Bash plus other POSIX utilities” is a reasonable choice, distinct from “POSIX utilities (including sh)”.
请注意,shell 编程涉及 shell 之外的其他实用程序。POSIX 指定了那些其他实用程序。“Bash 加其他 POSIX 实用程序”是一个合理的选择,区别于“POSIX 实用程序(包括 sh)”。
回答by Nowaker
Z shell (zsh)
Z 壳 (zsh)
It's said zsh is the most powerful for now so I would recommend trying it.
据说 zsh 是目前最强大的,所以我建议尝试一下。
- No matter which shell you learn - their syntax is very similar. Only built-in commands may slightly differ. But don't choose those old and unmaintained.
- Bash is the most popular. But almost every command in bash works in zsh the same way. There are some exceptions of course.
- AFAIK, every shell handles it the same way. But be warned - shells are stupid, they are not as smart as programming languages.
- I saw zsh working on all Linuxes, FreeBSD and OpenSolaris.
- See 4. Distros have zsh in their repos.
- 无论您学习哪种 shell - 它们的语法都非常相似。只有内置命令可能略有不同。但是不要选择那些旧的和未维护的。
- Bash 是最受欢迎的。但是 bash 中的几乎每个命令在 zsh 中的工作方式都相同。当然也有一些例外。
- AFAIK,每个外壳都以相同的方式处理它。但请注意——shell 是愚蠢的,它们不如编程语言聪明。
- 我看到 zsh 可以在所有 Linux、FreeBSD 和 OpenSolaris 上运行。
- 请参阅 4. 发行版在其存储库中有 zsh。
Why I prefer zsh (Z shell)to bash:
为什么我更喜欢zsh (Z shell)而不是bash:
- files matching like this:
for file in ./**/*.java; do ...
(I mean./**/*.ext
) - wants me to confirm when I do
rm *
:) - tab-autocompletion is a lot better, I can write
dmdomi[tab]
and it suggestsdnddomainname
.java
wants class name as the first parameter, zsh will suggest all classes available in the package and all subpackages.
- 像这样匹配的文件:(
for file in ./**/*.java; do ...
我的意思是./**/*.ext
) - 要我在做的时候确认
rm *
:) - 选项卡自动完成要好得多,我可以写
dmdomi[tab]
,它建议dnddomainname
.java
想要类名作为第一个参数,zsh 会建议包和所有子包中可用的所有类。
But you are not limited to zsh only. If something does not work for you, you just write it in bash or sh. This is what is "#!/bin/bash"
on top of the script for. :-)
但是您不仅限于 zsh。如果某些东西对您不起作用,您只需用 bash 或 sh 编写它。这就是"#!/bin/bash"
脚本之上的内容。:-)
To start quickly, use my .zshrc config: http://www.rozne.geozone.pl/.zshrcThe only thing you should change there is export LANG="pl_PL.UTF-8"
. You probably don't want Polish locale.
要快速开始,请使用我的 .zshrc 配置:http://www.rozne.geozone.pl/.zshrc唯一应该更改的是export LANG="pl_PL.UTF-8"
. 您可能不想要波兰语语言环境。
回答by sorpigal
Shell scripts for any *nix shell are generally deceptively simple. Easy things are usually easy, sometimes hard things are easy, sometimes easy-seeming things are hard. No shell is particularly better than the others in this area but some are worse (I can't seriously recommend csh). Some will say that bash is the worst 'modern' shell, which may be true but you can't completely escape it anyway.
任何 *nix shell 的 shell 脚本通常都看似简单。容易的事情往往容易,有时困难的事情容易,有时看似容易的事情却很难。在这方面,没有哪个 shell 比其他 shell 更好,但有些更糟(我不能认真推荐 csh)。有人会说 bash 是最糟糕的“现代”shell,这可能是真的,但无论如何你都无法完全摆脱它。
There's an argument to be made that using the most 'popular' shell is best for maintainability for the same reason Windows is best (and I'm not saying that it is): It's easy to find people you can hire who know how to use it. There are simply more people who have at least a passing familiarity with bash-specific features, say, than ksh or zsh. Finding people who actually understand what they're doing is another matter.
有一种观点认为,使用最“流行”的 shell 对可维护性来说是最好的,原因与 Windows 是最好的原因相同(我不是说它是):很容易找到可以雇用知道如何使用的人它。只是比 ksh 或 zsh 有更多的人至少对 bash 特定的功能有一定的熟悉度。找到真正理解他们在做什么的人是另一回事。
All shells have various gotchas, corner-cases and weird behaviors. Mostly it comes down to what you're used to. Shooting yourself in the foot is what I'd call a grand Unix tradition and no *nix shell can truly keep you safe.
所有 shell 都有各种陷阱、极端情况和奇怪的行为。主要取决于你的习惯。用脚射击自己是我所说的伟大的 Unix 传统,没有 *nix shell 可以真正保证你的安全。
Nearly every shell you'll see is highly portable to almost every platform. Even though this is true you won't necessarily be able to run the same (say) bash script on three different boxes unless you were careful about what utilities you used and which options you passed them. Writing portable shell scripts is hard for reasons having nothing to do with which shell they're written for.
您将看到的几乎每个 shell 对几乎所有平台都具有高度的可移植性。即使这是真的,您也不一定能够在三个不同的机器上运行相同的(比如说)bash 脚本,除非您小心使用了哪些实用程序以及传递了哪些选项。编写可移植的 shell 脚本很困难,原因与编写它们的 shell 无关。
Nearly every Linux uses bash by default and has most shells available. FreeBSD includes sh, csh and tcsh by default with bash and others in ports. Once upon a long time ago, Mac OS X used tcsh by default, but it now uses bash by default, and includes zsh along with most common shells. Beyond that I cannot comment.
默认情况下,几乎每个 Linux 都使用 bash,并且大多数 shell 可用。默认情况下,FreeBSD 包括 sh、csh 和 tcsh 以及 bash 和其他端口。很久以前,Mac OS X 默认使用 tcsh,但现在默认使用 bash,并且包括 zsh 和大多数常见的 shell。除此之外我无法发表评论。
Personally I use bash out of (mostly) inertia. If I weren't so familiar with it already I would use zsh instead.
就个人而言,我出于(主要是)惯性使用 bash。如果我对它还不太熟悉,我会改用 zsh。
回答by Tobu
bash is the standard and is very good at interactive use (good completion supporting many programs, history, readline support, many kinds of string expansion). It is also good at scripting, for a shell (arrays and hashes, quoting, string manipulation); though writing reliable scripts requires you to learn a lot more.
bash 是标准,非常擅长交互使用(良好的补全支持许多程序、历史记录、readline 支持、多种字符串扩展)。它也擅长脚本编写,用于 shell(数组和散列、引用、字符串操作);尽管编写可靠的脚本需要您学习更多。
If you want your programs to be able to grow, work with elaborate data structures, and use some useful libraries, you should learn a language like python, ruby or perl. Most of those have interactive interpreters as well, not as convenient as a shell but useful for quick testing. IPython, for Python, is particularly useful; it lets you explore documentation very easily, can load and reload source, includes a debugger. It also includes some standard shell commands and can pass the rest to a standard shell by prefixing them with a !
.
如果您希望您的程序能够增长、处理复杂的数据结构并使用一些有用的库,您应该学习 Python、ruby 或 perl 等语言。其中大多数也有交互式解释器,不像 shell 那样方便,但对快速测试很有用。IPython,对于 Python,特别有用;它让您可以非常轻松地浏览文档,可以加载和重新加载源代码,包括调试器。它还包括一些标准的 shell 命令,并且可以通过在它们前面加上前缀将其余命令传递给标准 shell !
。
- Thanks to being interactive most shells are easy enough to learn once you start using them exclusively
- I believe bash, and the posix subset, is better known by a wide margin. But the languages I mentioned are as well known as many shells.
- You can easily shoot yourself in the foot, convenience often makes undesirable things easy.
- and 5. Portability of the shell itself shouldn't be a problem; you may need to recompile to get more modern features on some of the OSes you mention. Using a full-blown language with its own libraries will help smoothe the variation of your multiplicity of platforms.
- 由于具有交互性,一旦您开始专门使用它们,大多数 shell 就很容易学习
- 我相信 bash 和 posix 子集更广为人知。但是我提到的语言和许多 shell 一样广为人知。
- 您可以轻松地用脚射击自己,方便往往使不受欢迎的事情变得容易。
- 5.外壳本身的便携性应该不成问题;您可能需要重新编译才能在您提到的某些操作系统上获得更多现代功能。使用具有自己的库的成熟语言将有助于平滑您的多种平台的变化。