bash、csh 与其他 - 哪个更适合应用程序维护?
可能的重复:
我应该使用什么 Linux shell?
我开始精通 Linux我正在尝试在命令 shell 脚本方面选择一个武器(因为我在这方面仍然是一个大菜鸟),这将帮助我(和其他人)管理、测试和管理一组正在运行的服务器端应用程序在 *NIX 环境中。
我的问题是:当考虑以下标准时,首选的命令 shell 是什么:
对于从未接触过 shell 脚本的初级开发人员来说,学习/理解有多容易? ?
是否有大量开发人员了解此 shell 脚本?
它是否安全且易于使用 - 脚本错误是否会保持沉默或提供智能错误输出,是否会让外行搬起石头砸自己的脚?
它的便携性如何? - 我可以期望相同的脚本在 OpenSolaris 以及 Redhat、FreeBSD 中运行吗? (特定操作系统的特定命令语法和选项将相应更改)
它的标准程度如何?它预计会包含在 *NIX 的大多数发行版中还是必须另外安装?
我知道有些阵营对特定的命令外壳抱有强烈的感情,我只是在寻找知情的意见。
Possible Duplicate:
What Linux shell should I use?
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.
My question is: What is(are) the preferred command shell(s) out there when the following criteria are considered:
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如今,几乎所有非嵌入式(或大型嵌入式)操作系统都有 POSIX :2001 又名单一 Unix v3 兼容层。它原生于 UNIX 平台(Linux、Mac OS X、Solaris、*BSD 等),并且可安装在其他平台(例如 Windows 和 Android)上。 POSIX 指定一种 shell 语言,通常称为 POSIX sh。这种语言源自 Bourne shell。
大多数 UNIX 系统都有 POSIX sh 的两种实现之一:ksh 或 bash,与 POSIX 相比,它们具有额外的有用功能。然而,一些不太主流的系统(尤其是嵌入式系统)可能只具有 POSIX 规定的功能。
鉴于您的目标,我看到三个选择:
忘记用于脚本编写的 csh,如果您想要通用的默认可用性,则忘记 zsh。
另请参阅主流 *NIX shell 之间的根本区别是什么?,特别是 < href="https://unix.stackexchange.com/questions/3320/what-are-the-fundamental-differences- Between-the-mainstream-nix-shells/3390#3390">我的答案。
请注意,shell 编程还涉及 shell 之外的其他实用程序。 POSIX 指定了其他实用程序。 “Bash 加上其他 POSIX 实用程序”是一个合理的选择,与“POSIX 实用程序(包括 sh)”不同。
These days, just about any non-embedded (or large embedded) operating system has a POSIX:2001 a.k.a. Single Unix v3 compatibility 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 shell language, usually known as POSIX sh. This language is derived from the 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.
Given your objectives, I see three choices:
Forget about csh for scripting, and forget about zsh if you want common default availability.
See also What are the fundamental differences between the mainstream *NIX shells?, particularly the “for scripting” part of my answer.
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)”.
csh 几乎总是错误。
csh is almost always wrong.
Z shell (zsh)
据说 zsh 是目前最强大的,所以我建议尝试一下。
为什么我更喜欢 zsh (Z shell) 而不是 bash:
for file in ./**/*.java; do ...
(我的意思是./**/*.ext
)rm *
时进行确认:)dmdomi[tab]
并且它建议dnddomainname
。java
希望类名作为第一个参数,zsh 将建议包和所有子包中可用的所有类。但您不仅限于 zsh。如果某些内容不适合您,您只需用 bash 或 sh 编写即可。这就是脚本顶部的
"#!/bin/bash"
的用途。 :-)要快速开始,请使用我的 .zshrc 配置: http://www.rozne.geozone.pl /.zshrc 您唯一应该更改的是
export LANG="pl_PL.UTF-8"
。您可能不想要波兰语言环境。Z shell (zsh)
It's said zsh is the most powerful for now so I would recommend trying it.
Why I prefer zsh (Z shell) to bash:
for file in ./**/*.java; do ...
(I mean./**/*.ext
)rm *
:)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.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. :-)To start quickly, use my .zshrc config: http://www.rozne.geozone.pl/.zshrc The only thing you should change there is
export LANG="pl_PL.UTF-8"
. You probably don't want Polish locale.任何 *nix shell 的 shell 脚本通常都非常简单。简单的事情通常很容易,有时困难的事情很容易,有时看似容易的事情却很难。在这个领域,没有哪个 shell 比其他 shell 特别好,但有些 shell 更差(我不能认真推荐 csh)。有人会说 bash 是最糟糕的“现代”shell,这可能是真的,但无论如何你都无法完全摆脱它。
有一种观点认为,使用最“流行”的 shell 最有利于可维护性,原因与 Windows 最好(我并不是说它是最好的)相同:很容易找到可以雇用的懂得如何使用的人它。与 ksh 或 zsh 相比,对 bash 特定功能至少有一定了解的人更多。找到真正了解自己在做什么的人是另一回事。
所有 shell 都有各种陷阱、极端情况和奇怪的行为。大多数情况下,这取决于您的习惯。搬起石头砸自己的脚,这就是我所说的伟大的 Unix 传统,没有一个 *nix shell 能够真正保证你的安全。
您将看到的几乎每个 shell 都可以高度移植到几乎所有平台。即使这是事实,您也不一定能够在三个不同的机器上运行相同的(例如)bash 脚本,除非您仔细了解您使用的实用程序以及传递给它们的选项。编写可移植的 shell 脚本很困难,因为它们与编写的 shell 无关。
几乎每个 Linux 都默认使用 bash,并且拥有大多数可用的 shell。 FreeBSD 默认情况下包括 sh、csh 和 tcsh,以及 ports 中的 bash 和其他命令。很久以前,Mac OS X 默认使用 tcsh,但现在默认使用 bash,并且包括 zsh 以及最常见的 shell。除此之外我无法发表评论。
就我个人而言,我使用 bash 是出于(主要)惯性。如果我还不太熟悉它,我会改用 zsh。
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.
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.
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.
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.
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.
Personally I use bash out of (mostly) inertia. If I weren't so familiar with it already I would use zsh instead.
bash 是标准,并且非常擅长交互使用(很好地完成支持许多程序、历史记录、readline 支持、多种字符串扩展)。它也擅长为 shell 编写脚本(数组和散列、引用、字符串操作);尽管编写可靠的脚本需要您学习更多知识。
如果您希望您的程序能够扩展、使用复杂的数据结构并使用一些有用的库,您应该学习 Python、Ruby 或 Perl 等语言。其中大多数也有交互式解释器,虽然不如 shell 方便,但对于快速测试很有用。 IPython,对于Python来说,特别有用;它可以让您轻松浏览文档,可以加载和重新加载源代码,包括一个调试器。它还包括一些标准 shell 命令,并且可以通过在命令前面添加
!
将其余命令传递给标准 shell。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.
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
!
.