bash、csh 与其他 - 哪个更适合应用程序维护?

发布于 2024-10-04 20:43:21 字数 701 浏览 9 评论 0原文

可能的重复:
我应该使用什么 Linux shell?

我开始精通 Linux我正在尝试在命令 shell 脚本方面选择一个武器(因为我在这方面仍然是一个大菜鸟),这将帮助我(和其他人)管理、测试和管理一组正在运行的服务器端应用程序在 *NIX 环境中。

我的问题是:当考虑以下标准时,首选的命令 shell 是什么:

  1. 对于从未接触过 shell 脚本的初级开发人员来说,学习/理解有多容易? ?

  2. 是否有大量开发人员了解此 shell 脚本?

  3. 它是否安全且易于使用 - 脚本错误是否会保持沉默或提供智能错误输出,是否会让外行搬起石头砸自己的脚?

  4. 它的便携性如何? - 我可以期望相同的脚本在 OpenSolaris 以及 Redhat、FreeBSD 中运行吗? (特定操作系统的特定命令语法和选项将相应更改)

  5. 它的标准程度如何?它预计会包含在 *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:

  1. How easy is it to learn/understand for a junior dev who has never had an exposure to shell scripting?

  2. Is there a big pool of developers out there that know this shell script?

  3. 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?

  4. 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)

  5. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

執念 2024-10-11 20:43:21

如今,几乎所有非嵌入式(或大型嵌入式)操作系统都有 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 规定的功能。

鉴于您的目标,我看到三个选择:

  • 限制自己使用 POSIX sh。优点:您不必担心不同的变体,因为有一个标准且兼容的实现很容易获得。缺点:您无法从 bash 和 ksh 的扩展中受益。
  • 使用 ksh 和 bash 的交集。这在外观上很吸引人,但这确实意味着您必须使用两个参考文档而不是一个 - 甚至 bash 和 ksh 的共同功能并不总是使用相同的语法。弄清楚要在给定系统上使用哪一个也是一件痛苦的事情。
  • 选择 ksh 或 bash 之一。 bash 和 ksh 都可以在所有类 UNIX 平台和 Windows 上使用。两者都有一个开源实现(唯一一个用于 bash,ATT ksh93 用于 ksh),可以安装在大多数平台上。我会选择 bash 而不是 ksh 有两个原因。首先,它是 Linux 上的默认设置,因此您会发现更多的人习惯了它。其次,有些系统附带了较旧的、功能较少的 ksh 实现;即使你可以安装ksh93,这也是你部署时要考虑的另一件事。

忘记用于脚本编写的 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:

  • 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.

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)”.

初吻给了烟 2024-10-11 20:43:21

csh 几乎总是错误

csh is almost always wrong.

热血少△年 2024-10-11 20:43:21

Z shell (zsh)

据说 zsh 是目前最强大的,所以我建议尝试一下。

  1. 无论您学习哪种 shell,它们的语法都非常相似。仅内置命令可能略有不同。但不要选择那些陈旧且无人维护的。
  2. Bash 是最受欢迎的。但 bash 中的几乎每个命令在 zsh 中的工作方式都是相同的。当然也有一些例外。
  3. AFAIK,每个 shell 都以相同的方式处理它。但请注意 - shell 很愚蠢,它们不像编程语言那么聪明。
  4. 我看到 zsh 可以在所有 Linux、FreeBSD 和 OpenSolaris 上运行。
  5. 请参阅 4。发行版在其存储库中包含 zsh。

为什么我更喜欢 zsh (Z shell) 而不是 bash

  • 像这样匹配的文件:for file in ./**/*.java; do ... (我的意思是 ./**/*.ext
  • 希望我在执行 rm * 时进行确认:)
  • tab-autocompletion 是一个好多了,我可以写 dmdomi[tab] 并且它建议 dnddomainnamejava 希望类名作为第一个参数,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.

  1. 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.
  2. Bash is the most popular. But almost every command in bash works in zsh the same way. There are some exceptions of course.
  3. AFAIK, every shell handles it the same way. But be warned - shells are stupid, they are not as smart as programming languages.
  4. I saw zsh working on all Linuxes, FreeBSD and OpenSolaris.
  5. See 4. Distros have zsh in their repos.

Why I prefer zsh (Z shell) to 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 suggests dnddomainname. 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.

夜唯美灬不弃 2024-10-11 20:43:21

任何 *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.

压抑⊿情绪 2024-10-11 20:43:21

bash 是标准,并且非常擅长交互使用(很好地完成支持许多程序、历史记录、readline 支持、多种字符串扩展)。它也擅长为 shell 编写脚本(数组和散列、引用、字符串操作);尽管编写可靠的脚本需要您学习更多知识。

如果您希望您的程序能够扩展、使用复杂的数据结构并使用一些有用的库,您应该学习 Python、Ruby 或 Perl 等语言。其中大多数也有交互式解释器,虽然不如 shell 方便,但对于快速测试很有用。 IPython,对于Python来说,特别有用;它可以让您轻松浏览文档,可以加载和重新加载源代码,包括一个调试器。它还包括一些标准 shell 命令,并且可以通过在命令前面添加 ! 将其余命令传递给标准 shell。

  1. 由于具有交互性,一旦您开始专门使用大多数 shell,它们就很容易学习,
  2. 我相信 bash 和 posix 子集更广为人知。但我提到的语言和许多 shell 一样众所周知。
  3. 你很容易搬起石头砸自己的脚,便利往往会让不受欢迎的事情变得容易发生。
  4. 5. 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 !.

  1. Thanks to being interactive most shells are easy enough to learn once you start using them exclusively
  2. 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.
  3. You can easily shoot yourself in the foot, convenience often makes undesirable things easy.
  4. 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文