Bash脚本编程还有什么用吗?

时间:2021-02-24 22:24:37

I just finished my second year as a university CS student, so my "real-world" knowledge is lacking. I learned Java my first year, continued with Java and picked up C and simple Bash scripting my second. This summer I'm trying to learn Perl (God help me). I've dabbled with Python a bit in the past.

我刚刚完成了我的第二年的大学CS学生,所以我的“真实世界”知识是缺乏的。我第一年学习Java,第二年继续学习Java,第二年学习了C和简单的Bash脚本。今年夏天,我试图学习Perl(上帝保佑我)。过去我曾涉猎过Python。

My question is, now that we have very readable, very writable scripting languages like Python, Ruby, Perl, etc, why does anyone write Bash scripts? Is there something I'm missing? I know my linux box has perl and python. Are they not ubiquitous enough? Is there really something that's easier to do in Bash than in some other hll?

我的问题是,既然我们已经有了非常可读、非常可写的脚本语言,比如Python、Ruby、Perl等等,为什么有人要编写Bash脚本呢?我是不是漏掉了什么?我知道我的linux box有perl和python。它们还不够普遍吗?在Bash中确实有比在其他hll中更容易做的事情吗?

18 个解决方案

#1


51  

Among other things, bash is useful when most of what you're doing is communicating and piping between various programs (many of which are also standard). And there are many environments where bash (or at least a POSIX shell) is available but Perl and Python are not.

在其他事情中,bash非常有用,因为您所做的大部分工作是在不同的程序之间进行通信和管道传输(其中许多程序也是标准的)。有很多环境可以使用bash(或至少POSIX shell),但是Perl和Python没有。

And of course, you should distinguish between interactive bash and scripted bash. Ubuntu recently switched their default scripted shell (#!/bin/sh) to dash because it was much faster. However, bash has useful interactive features dash does not (zsh is still better, IMHO).

当然,您应该区分交互式bash和脚本bash。Ubuntu最近将其默认脚本shell (#!/bin/sh)切换到dash,因为它要快得多。然而,bash有一些有用的交互特性,而dash没有(zsh更好,IMHO)。

See also Is it worth learning BASH when I know Perl?

还可以看到,当我了解Perl时,学习BASH是否值得?

#2


28  

Bash is incredibly useful in system administration, web application deployment, data crunching, sutomated backups, even getting-things-done day by day management just to name really few of them. I think It's too early for you to judge a "veteran IT soldier" like BASH.

Bash在系统管理、web应用程序部署、数据处理、子备份、甚至是日常管理中非常有用,仅举几个例子。我认为现在就判断一个像巴什这样的“老兵It军人”还为时过早。

EDIT googling around:

编辑搜索:

I'd like also to mention that TextMate a successful OS-X program has a lot of Bash machinery inside.

我还想提一下TextMate一个成功的OS-X程序里面有很多Bash机制。

#3


21  

The real difference between bash and python is that python is a general purpose scripting language, while bash is simply a way to run a myriad of small (and often very fast) programs in a series. Python can do this, but it is not optimized for it. The programs (sort, find, uniq, scp) might do very complex tasks very simply, and bash allows these tasks to interoperate very simply with piping, flushing output in and out from files or devices etc.

bash和python之间的真正区别在于,python是一种通用的脚本语言,而bash只是一种在系列中运行大量小程序(通常是非常快的)的方法。Python可以做到这一点,但它并没有对它进行优化。程序(sort、find、uniq、scp)可能非常简单地执行非常复杂的任务,而bash允许这些任务与管道、文件或设备的输出进行交互,等等。

While Python can run the same programs, you will be forced to do bash scripting in the python script to accomplish the same thing, and then you are stuck with both python and bash. Both are fine by them self, but a mix of these don't improve anything IMHO.

虽然Python可以运行相同的程序,但是您将*在Python脚本中执行bash脚本以完成相同的事情,然后您将同时使用Python和bash。两者本身都很好,但混合在一起并不能改善任何东西。

#4


21  

Bash scripts allow you to automate command line tasks by using the same language you would if you type the commands out manually.

Bash脚本允许您使用与手动输入命令相同的语言来自动执行命令行任务。

Trivial Example to list $PATH

Bash

#!/bin/sh
echo $PATH

Python

import os
print os.getenv("path")

#5


11  

For a lot of tasks, bash can be very very concise. Code to search for a string in all the files of the current directory and print those lines:

对于许多任务,bash可以非常非常简洁。在当前目录的所有文件中搜索字符串并打印这些行:

In bash:

在bash中:

grep -ir "search_term" .

In Python:

在Python中:

for file in os.listdir('.'):
  file_content = open(file).readlines().split('\n')
  for line in file_content:
    if line.count("search_term") > 0:
      print file, ": ", line

#6


8  

I think that a strong argument that python is not as natural as bash for many tasks is the simple fact that no one uses the python interpreter as their shell. Clearly this is possible:

我认为,关于python对于许多任务来说没有bash那么自然的一个强有力的论点是,没有人使用python解释器作为他们的shell。很明显,这是可能的:

python> import os, subprocess
python> os.chdir(os.path.expanduser("~/work"))
python> subprocess.call(["vim","README"])

Clearly using python for a shell is absurd. Bash was designed for running other programs. Python was designed for doing computations. While, most things can be done in either language, there will always be tasks that can be done more easily in bash than in python and vice versa.

显然,使用python作为shell是荒谬的。Bash设计用于运行其他程序。Python是为做计算而设计的。虽然,大多数事情都可以用这两种语言完成,但是总有一些任务可以在bash中比在python中更容易完成,反之亦然。

#7


6  

Well, when writing with bash, you can directly use every possible tool you have on the command line for your script. With any other language you would first have to execute that command and get some result etc. A simple script that (for example) gets a list of processes, runs through grep and gets some result would be a lot more complicated in other languages. As such, bash is still a good tool for writing quick things.

当使用bash编写时,您可以直接使用脚本的命令行上的所有可能的工具。对于任何其他语言,您首先都必须执行该命令并获得一些结果等等。一个简单的脚本(例如)获得一个进程列表,通过grep运行并获得一些结果,在其他语言中可能要复杂得多。因此,bash仍然是编写快速事物的好工具。

#8


4  

Many startup scripts are traditionally written as shell scripts and there doesn't seem to be a trend to move away from those.

许多启动脚本传统上是作为shell脚本编写的,而且似乎没有趋势要离开这些脚本。

Shell scripts are perfectly suited for starting other processes and gluing their input/output together. That's an area where the other scripting languages are not as powerful.

Shell脚本非常适合启动其他进程并将它们的输入/输出粘接在一起。这是其他脚本语言没有那么强大的地方。

Apart from that: even if languages like Perl, Python and Ruby become more and more ubiquitous, the only language that you're practically guaranteed on every unix-like system is the bourne shell ('though not necessarily in the form of bash) .

除此之外:即使像Perl、Python和Ruby这样的语言变得越来越普遍,实际上在每个类unix系统上,惟一可以保证的语言就是bourne shell(“尽管不一定是以bash的形式)。”

#9


4  

Apart from what others have said, I'd like to point out what's in my opinion the main reason to learn Bash: it's the (almost) standard Linux shell.

除了其他人所说的之外,我还想指出我认为学习Bash的主要原因:它是(几乎)标准的Linux shell。

Other scripting languages are surely useful, and maybe a lot more powerful, but what you'll be dealing with when you have a terminal in front of you is... Bash.

其他脚本语言当然是有用的,可能更强大,但是当你面前有一个终端时,你要处理的是……Bash。

Being able to manage I/O, pipes and processes, assing and use variables, and do at least some loop and condition evaluation is a must, if you want to manage a Linux system.

如果您想管理一个Linux系统,那么能够管理I/O、管道和流程、收集和使用变量,并且至少做一些循环和条件评估是必须的。

#10


2  

Easier, probably not. I actually prefer perl to bash scripting in many cases. Bash does have one advantage, though, especially on Linux systems: it's all but guaranteed to be installed. And if it's not, its largely-compatible father (sh) will be, cause almost all system scripts are written for sh. Even perl isn't that ubiquitous, and it's everyfreakingwhere.

容易,这是不可能的。在很多情况下,我更喜欢perl而不是bash脚本。不过,Bash确实有一个优点,尤其是在Linux系统上:它几乎肯定会被安装。如果它不是,它的兼容的父亲(sh)将会是,因为几乎所有的系统脚本都是为sh编写的,甚至perl也不是那么普遍,而且它是每个地方都有。

#11


2  

Bash is not only a scripting language, but it is a shell.

Bash不仅是一种脚本语言,而且还是一种shell。

Job control languages and shells
Main article: Shell script

作业控制语言和Shell主要文章:Shell脚本

A major class of scripting languages has grown out of the automation of job control, which relates to starting and controlling the behavior of system programs. (In this sense, one might think of shells as being descendants of IBM's JCL, or Job Control Language, which was used for exactly this purpose.) Many of these languages' interpreters double as command-line interpreters such as the Unix shell or the MS-DOS COMMAND.COM. Others, such as AppleScript offer the use of English-like commands to build scripts. This combined with Mac OS X's Cocoa frameworks allows user to build entire applications using AppleScript & Cocoa objects.

一种主要的脚本语言已经从工作控制的自动化中发展出来,它与启动和控制系统程序的行为有关。(从这个意义上说,您可能认为shell是IBM的JCL(作业控制语言)的后代,该语言正是用于此目的的。)这些语言中的许多解释器都是命令行解释器,如Unix shell或MS-DOS COMMAND.COM。其他的,如AppleScript提供了使用类似英语的命令来构建脚本。这与Mac OS X的Cocoa框架相结合,允许用户使用AppleScript和Cocoa对象构建整个应用程序。

Python, Ruby and Perl are great, but are more general tools; in certain cases (embedded devices or other minimalistic systems) they can be considered bloat or in other situations they might pose a security risk (for environments where you want very high security and try to eliminate any unnecessary package).

Python、Ruby和Perl非常好,但是它们是更通用的工具;在某些情况下(嵌入式设备或其他最小化的系统),它们可能被认为是膨胀的,或者在其他情况下,它们可能会带来安全风险(对于需要非常高的安全性并试图消除任何不必要的包的环境)。

In some cases shell scripts will perform much better due to closer integration with OS.

在某些情况下,由于与操作系统的更紧密集成,shell脚本将执行得更好。

Also, bash is production proven for myriad of administrative tasks and it is easy to find scripts that will cover very complicated scenarios (exceptions) and handle them gracefully.

而且,bash已经为无数的管理任务提供了产品证明,并且很容易找到能够覆盖非常复杂的场景(异常)的脚本并优雅地处理它们。

#12


2  

If you do lots of GUI stuff, you'll probably only meet bash whenever you're doing some sort of customization on your own machine. Various hacks and stuff. If you use the command line to do stuff, bash is just indispensable. In fact, being good on the command line requires bash or some other shell familiarity.

如果您做了大量的GUI工作,那么当您在自己的机器上做一些定制时,您可能只会遇到bash。各种黑客和东西。如果您使用命令行来做一些事情,bash是必不可少的。事实上,在命令行上表现良好需要bash或其他shell熟悉。

I get miles out of having learned Bash when I wanted to navigate around my harddrive quickly. I wrote a navigation/menu interface that let me beam to different folders and files quickly and easily. Writing it in bash was simple and easy. And there's lots of easily accessed, and free, stuff that'll show you how.

当我想要快速地在我的哈德里韦河上航行时,我从学了Bash中学到了很多东西。我编写了一个导航/菜单界面,让我可以快速、轻松地发送到不同的文件夹和文件。用bash编写它既简单又容易。有很多很容易获取的免费的东西,会告诉你怎么做。

Also, learning Bash is great for understanding how Unix and some of the core stuff really works -- and how far we've come with tools like Python.

此外,学习Bash对于了解Unix和一些核心内容是如何工作的,以及我们使用Python等工具已经走了多远是很有帮助的。

#13


1  

Bash (and the original Bourne sh and myriad derivatives) is - from one perspective - an incredibly high-level language. Where many languages use simple primitives, shell primitives are entire programs.

Bash(以及原始的Bourne sh和myriad衍生物)是一种非常高级的语言。许多语言使用简单的原语,shell原语是整个程序。

That it might not be the best language to express your tasks, doesn't mean it is dead, dying, or even moribund.

它可能不是表达你的任务的最佳语言,但并不意味着它已经死了、奄奄一息、甚至奄奄一息。

#14


1  

You may get a true bournesh over at freshmeat (on Mac OS X, for example, /bin/sh is not a true bournesh).

你可能会在鲜肉(比如在Mac OS X上,/bin/sh不是真正的bournesh)上吃到真正的bournesh。

How to identify a traditional Bourne shell? ... A simple check for an often undocumented but characteristic feature: You can use the circumflex ^ (caret) as replacement for | (pipe).

如何识别传统的伯恩壳?…一个简单的检查一个经常非法但特征:您可以使用弯曲的^(脱字符号)作为替代|(管)。

see: http://www.in-ulm.de/~mascheck/bourne/

见:http://www.in-ulm.de/伯恩~ mascheck / /

#15


1  

In my experience, Perl meets something like 99% of any need that might require a shell script. As a bonus, it is possible to write code that runs on Windows sans Cygwin. If I won't have a Perl install on a Windows box I want to target, I can use PAR::Packer or PerlApp to produce an executable. Python, Ruby and others should work just as well, too.

在我的经验中,Perl满足了99%的可能需要shell脚本的需求。此外,还可以编写运行在没有Cygwin的Windows上的代码。如果我不想在我要针对的Windows box上安装Perl,我可以使用PAR: Packer或PerlApp来生成可执行文件。Python、Ruby和其他语言也应该同样有效。

However, shell scripting isn't all that complicated--at least things that you should be scripting in a shell aren't all that complicated. You can do what you need to do with a fairly shallow level of knowledge.

但是,shell脚本并不是那么复杂——至少在shell中应该编写的东西并不那么复杂。你可以做你需要做的事情,你需要一个相当肤浅的知识水平。

Learn how to read and set variables. How to create and call functions. How to source other files. Learn how flow control works.

学习如何读取和设置变量。如何创建和调用函数。如何找到其他文件。了解流控制是如何工作的。

And most important, learn to read the shell man page. This may sound facetious, but I am 100% serious--don't worry about cramming every detail of shell scripting into your brain, instead learn to find what you need to know in the man page quickly and efficiently. If you find yourself using shell scripting often, the pertinent info will naturally stick in your brain.

最重要的是,学会阅读shell man页面。这听起来可能有点滑稽,但我是100%认真的——不要担心把shell脚本的每个细节都塞进你的脑子里,而是要学会快速有效地在手册页中找到你需要知道的东西。如果您发现自己经常使用shell脚本,那么相关的信息将自然而然地留在您的脑海中。

So, yes, basic shell is worth learning.

所以,是的,基本的外壳是值得学习的。

#16


1  

I'm a perl guy, but the number of the bash (or ksh) functions I use and create on a daily basis is quite significant. For anything involved, I'll write a perl script, but for navigating the directory structure, and specifically for manipulating environment variables bash/ksh/... are indispensable.

我是perl高手,但是我每天使用和创建的bash(或ksh)函数的数量是非常重要的。对于涉及的任何内容,我将编写一个perl脚本,但是用于导航目录结构,特别是用于操作环境变量bash/ksh/…是不可或缺的。

Again, especially for environment variables nothing beats shell, and quite a few programs use environment variables. In Perl, I have to write a bash alias or function that calls the Perl script, which writes out a temporary bash script, which then gets sourced after Perl exits in order to make the change in the same environment I'm launching from.

再说一遍,特别是对于环境变量,没有什么比shell更好的了,很多程序都使用环境变量。在Perl中,我必须编写一个bash别名或函数来调用Perl脚本,该脚本编写一个临时bash脚本,然后在Perl退出后调用该脚本,以便在我正在启动的相同环境中进行更改。

I've done this, especially for heavy-lifting on path variables. But there's no way to do it in just Perl (or python or ruby... or C-code for that matter).

我已经做过了,特别是对于路径变量的重载。但是,仅仅用Perl(或python或ruby……)或者c代码)

#17


1  

As mentioned, the GNU tools are great, and are easiest to use within the shell. It is especially nice if your data is already in a linear or tabular form of plain text. Just as an example, the other day I was able to build a script to create an XHTML word cloud of any text file in 8 lines of Bourne Shell, which is even less powerful (but more widely supported) than Bash.

如前所述,GNU工具非常棒,并且在shell中最容易使用。如果您的数据已经是纯文本的线性或表格形式,那就更棒了。例如,前几天我构建了一个脚本,在Bourne Shell的8行中创建任何文本文件的XHTML字云,它的功能甚至不如Bash强大(但得到了更广泛的支持)。

#18


1  

What I don't get is why people say bash when they mean any bourne-shell compatible shell.

我不明白为什么人们说bash指的是任何与bourne-shell兼容的shell。

When writing shell scripts: always try to use constructs that also work in older bourne shell interpreters as well. It will save you lots of trouble some day.

在编写shell脚本时:总是尝试使用同样适用于旧的bourne shell解释器的构造。总有一天它会为你省去许多麻烦。

And yes, there is plenty of use for shell scripts today, as the shell always exist on all unixes, out of the box, contrary to perl, python, csh, zsh, ksh (possibly?), and so on. Most of the time they only add extra convenience or different syntax for constructs like loops and tests. Some have improved redirection features.

是的,现在shell脚本有很多用途,因为shell始终存在于所有unix上,开箱即用,这与perl、python、csh、zsh、ksh(可能吗?)等不同。大多数情况下,它们只会为诸如循环和测试这样的结构添加额外的便利或不同的语法。有些改进了重定向功能。

Most of the time, I would say that ordinary bourne shell works equally well.

大多数时候,我想说的是,普通的伯恩·贝克尔也同样出色。

Typical pitfall: if ! test $x -eq $y works as expected in bash that has a more clever builtin "if" operator, but the "correct" if test ! $x -eq $y should work in all environments.

典型的陷阱:如果!测试$x -eq $y在bash中如预期的那样工作,它有一个更聪明的builtin“如果”操作符,但是如果测试“正确”的话!$x -eq $y应该适用于所有环境。

#1


51  

Among other things, bash is useful when most of what you're doing is communicating and piping between various programs (many of which are also standard). And there are many environments where bash (or at least a POSIX shell) is available but Perl and Python are not.

在其他事情中,bash非常有用,因为您所做的大部分工作是在不同的程序之间进行通信和管道传输(其中许多程序也是标准的)。有很多环境可以使用bash(或至少POSIX shell),但是Perl和Python没有。

And of course, you should distinguish between interactive bash and scripted bash. Ubuntu recently switched their default scripted shell (#!/bin/sh) to dash because it was much faster. However, bash has useful interactive features dash does not (zsh is still better, IMHO).

当然,您应该区分交互式bash和脚本bash。Ubuntu最近将其默认脚本shell (#!/bin/sh)切换到dash,因为它要快得多。然而,bash有一些有用的交互特性,而dash没有(zsh更好,IMHO)。

See also Is it worth learning BASH when I know Perl?

还可以看到,当我了解Perl时,学习BASH是否值得?

#2


28  

Bash is incredibly useful in system administration, web application deployment, data crunching, sutomated backups, even getting-things-done day by day management just to name really few of them. I think It's too early for you to judge a "veteran IT soldier" like BASH.

Bash在系统管理、web应用程序部署、数据处理、子备份、甚至是日常管理中非常有用,仅举几个例子。我认为现在就判断一个像巴什这样的“老兵It军人”还为时过早。

EDIT googling around:

编辑搜索:

I'd like also to mention that TextMate a successful OS-X program has a lot of Bash machinery inside.

我还想提一下TextMate一个成功的OS-X程序里面有很多Bash机制。

#3


21  

The real difference between bash and python is that python is a general purpose scripting language, while bash is simply a way to run a myriad of small (and often very fast) programs in a series. Python can do this, but it is not optimized for it. The programs (sort, find, uniq, scp) might do very complex tasks very simply, and bash allows these tasks to interoperate very simply with piping, flushing output in and out from files or devices etc.

bash和python之间的真正区别在于,python是一种通用的脚本语言,而bash只是一种在系列中运行大量小程序(通常是非常快的)的方法。Python可以做到这一点,但它并没有对它进行优化。程序(sort、find、uniq、scp)可能非常简单地执行非常复杂的任务,而bash允许这些任务与管道、文件或设备的输出进行交互,等等。

While Python can run the same programs, you will be forced to do bash scripting in the python script to accomplish the same thing, and then you are stuck with both python and bash. Both are fine by them self, but a mix of these don't improve anything IMHO.

虽然Python可以运行相同的程序,但是您将*在Python脚本中执行bash脚本以完成相同的事情,然后您将同时使用Python和bash。两者本身都很好,但混合在一起并不能改善任何东西。

#4


21  

Bash scripts allow you to automate command line tasks by using the same language you would if you type the commands out manually.

Bash脚本允许您使用与手动输入命令相同的语言来自动执行命令行任务。

Trivial Example to list $PATH

Bash

#!/bin/sh
echo $PATH

Python

import os
print os.getenv("path")

#5


11  

For a lot of tasks, bash can be very very concise. Code to search for a string in all the files of the current directory and print those lines:

对于许多任务,bash可以非常非常简洁。在当前目录的所有文件中搜索字符串并打印这些行:

In bash:

在bash中:

grep -ir "search_term" .

In Python:

在Python中:

for file in os.listdir('.'):
  file_content = open(file).readlines().split('\n')
  for line in file_content:
    if line.count("search_term") > 0:
      print file, ": ", line

#6


8  

I think that a strong argument that python is not as natural as bash for many tasks is the simple fact that no one uses the python interpreter as their shell. Clearly this is possible:

我认为,关于python对于许多任务来说没有bash那么自然的一个强有力的论点是,没有人使用python解释器作为他们的shell。很明显,这是可能的:

python> import os, subprocess
python> os.chdir(os.path.expanduser("~/work"))
python> subprocess.call(["vim","README"])

Clearly using python for a shell is absurd. Bash was designed for running other programs. Python was designed for doing computations. While, most things can be done in either language, there will always be tasks that can be done more easily in bash than in python and vice versa.

显然,使用python作为shell是荒谬的。Bash设计用于运行其他程序。Python是为做计算而设计的。虽然,大多数事情都可以用这两种语言完成,但是总有一些任务可以在bash中比在python中更容易完成,反之亦然。

#7


6  

Well, when writing with bash, you can directly use every possible tool you have on the command line for your script. With any other language you would first have to execute that command and get some result etc. A simple script that (for example) gets a list of processes, runs through grep and gets some result would be a lot more complicated in other languages. As such, bash is still a good tool for writing quick things.

当使用bash编写时,您可以直接使用脚本的命令行上的所有可能的工具。对于任何其他语言,您首先都必须执行该命令并获得一些结果等等。一个简单的脚本(例如)获得一个进程列表,通过grep运行并获得一些结果,在其他语言中可能要复杂得多。因此,bash仍然是编写快速事物的好工具。

#8


4  

Many startup scripts are traditionally written as shell scripts and there doesn't seem to be a trend to move away from those.

许多启动脚本传统上是作为shell脚本编写的,而且似乎没有趋势要离开这些脚本。

Shell scripts are perfectly suited for starting other processes and gluing their input/output together. That's an area where the other scripting languages are not as powerful.

Shell脚本非常适合启动其他进程并将它们的输入/输出粘接在一起。这是其他脚本语言没有那么强大的地方。

Apart from that: even if languages like Perl, Python and Ruby become more and more ubiquitous, the only language that you're practically guaranteed on every unix-like system is the bourne shell ('though not necessarily in the form of bash) .

除此之外:即使像Perl、Python和Ruby这样的语言变得越来越普遍,实际上在每个类unix系统上,惟一可以保证的语言就是bourne shell(“尽管不一定是以bash的形式)。”

#9


4  

Apart from what others have said, I'd like to point out what's in my opinion the main reason to learn Bash: it's the (almost) standard Linux shell.

除了其他人所说的之外,我还想指出我认为学习Bash的主要原因:它是(几乎)标准的Linux shell。

Other scripting languages are surely useful, and maybe a lot more powerful, but what you'll be dealing with when you have a terminal in front of you is... Bash.

其他脚本语言当然是有用的,可能更强大,但是当你面前有一个终端时,你要处理的是……Bash。

Being able to manage I/O, pipes and processes, assing and use variables, and do at least some loop and condition evaluation is a must, if you want to manage a Linux system.

如果您想管理一个Linux系统,那么能够管理I/O、管道和流程、收集和使用变量,并且至少做一些循环和条件评估是必须的。

#10


2  

Easier, probably not. I actually prefer perl to bash scripting in many cases. Bash does have one advantage, though, especially on Linux systems: it's all but guaranteed to be installed. And if it's not, its largely-compatible father (sh) will be, cause almost all system scripts are written for sh. Even perl isn't that ubiquitous, and it's everyfreakingwhere.

容易,这是不可能的。在很多情况下,我更喜欢perl而不是bash脚本。不过,Bash确实有一个优点,尤其是在Linux系统上:它几乎肯定会被安装。如果它不是,它的兼容的父亲(sh)将会是,因为几乎所有的系统脚本都是为sh编写的,甚至perl也不是那么普遍,而且它是每个地方都有。

#11


2  

Bash is not only a scripting language, but it is a shell.

Bash不仅是一种脚本语言,而且还是一种shell。

Job control languages and shells
Main article: Shell script

作业控制语言和Shell主要文章:Shell脚本

A major class of scripting languages has grown out of the automation of job control, which relates to starting and controlling the behavior of system programs. (In this sense, one might think of shells as being descendants of IBM's JCL, or Job Control Language, which was used for exactly this purpose.) Many of these languages' interpreters double as command-line interpreters such as the Unix shell or the MS-DOS COMMAND.COM. Others, such as AppleScript offer the use of English-like commands to build scripts. This combined with Mac OS X's Cocoa frameworks allows user to build entire applications using AppleScript & Cocoa objects.

一种主要的脚本语言已经从工作控制的自动化中发展出来,它与启动和控制系统程序的行为有关。(从这个意义上说,您可能认为shell是IBM的JCL(作业控制语言)的后代,该语言正是用于此目的的。)这些语言中的许多解释器都是命令行解释器,如Unix shell或MS-DOS COMMAND.COM。其他的,如AppleScript提供了使用类似英语的命令来构建脚本。这与Mac OS X的Cocoa框架相结合,允许用户使用AppleScript和Cocoa对象构建整个应用程序。

Python, Ruby and Perl are great, but are more general tools; in certain cases (embedded devices or other minimalistic systems) they can be considered bloat or in other situations they might pose a security risk (for environments where you want very high security and try to eliminate any unnecessary package).

Python、Ruby和Perl非常好,但是它们是更通用的工具;在某些情况下(嵌入式设备或其他最小化的系统),它们可能被认为是膨胀的,或者在其他情况下,它们可能会带来安全风险(对于需要非常高的安全性并试图消除任何不必要的包的环境)。

In some cases shell scripts will perform much better due to closer integration with OS.

在某些情况下,由于与操作系统的更紧密集成,shell脚本将执行得更好。

Also, bash is production proven for myriad of administrative tasks and it is easy to find scripts that will cover very complicated scenarios (exceptions) and handle them gracefully.

而且,bash已经为无数的管理任务提供了产品证明,并且很容易找到能够覆盖非常复杂的场景(异常)的脚本并优雅地处理它们。

#12


2  

If you do lots of GUI stuff, you'll probably only meet bash whenever you're doing some sort of customization on your own machine. Various hacks and stuff. If you use the command line to do stuff, bash is just indispensable. In fact, being good on the command line requires bash or some other shell familiarity.

如果您做了大量的GUI工作,那么当您在自己的机器上做一些定制时,您可能只会遇到bash。各种黑客和东西。如果您使用命令行来做一些事情,bash是必不可少的。事实上,在命令行上表现良好需要bash或其他shell熟悉。

I get miles out of having learned Bash when I wanted to navigate around my harddrive quickly. I wrote a navigation/menu interface that let me beam to different folders and files quickly and easily. Writing it in bash was simple and easy. And there's lots of easily accessed, and free, stuff that'll show you how.

当我想要快速地在我的哈德里韦河上航行时,我从学了Bash中学到了很多东西。我编写了一个导航/菜单界面,让我可以快速、轻松地发送到不同的文件夹和文件。用bash编写它既简单又容易。有很多很容易获取的免费的东西,会告诉你怎么做。

Also, learning Bash is great for understanding how Unix and some of the core stuff really works -- and how far we've come with tools like Python.

此外,学习Bash对于了解Unix和一些核心内容是如何工作的,以及我们使用Python等工具已经走了多远是很有帮助的。

#13


1  

Bash (and the original Bourne sh and myriad derivatives) is - from one perspective - an incredibly high-level language. Where many languages use simple primitives, shell primitives are entire programs.

Bash(以及原始的Bourne sh和myriad衍生物)是一种非常高级的语言。许多语言使用简单的原语,shell原语是整个程序。

That it might not be the best language to express your tasks, doesn't mean it is dead, dying, or even moribund.

它可能不是表达你的任务的最佳语言,但并不意味着它已经死了、奄奄一息、甚至奄奄一息。

#14


1  

You may get a true bournesh over at freshmeat (on Mac OS X, for example, /bin/sh is not a true bournesh).

你可能会在鲜肉(比如在Mac OS X上,/bin/sh不是真正的bournesh)上吃到真正的bournesh。

How to identify a traditional Bourne shell? ... A simple check for an often undocumented but characteristic feature: You can use the circumflex ^ (caret) as replacement for | (pipe).

如何识别传统的伯恩壳?…一个简单的检查一个经常非法但特征:您可以使用弯曲的^(脱字符号)作为替代|(管)。

see: http://www.in-ulm.de/~mascheck/bourne/

见:http://www.in-ulm.de/伯恩~ mascheck / /

#15


1  

In my experience, Perl meets something like 99% of any need that might require a shell script. As a bonus, it is possible to write code that runs on Windows sans Cygwin. If I won't have a Perl install on a Windows box I want to target, I can use PAR::Packer or PerlApp to produce an executable. Python, Ruby and others should work just as well, too.

在我的经验中,Perl满足了99%的可能需要shell脚本的需求。此外,还可以编写运行在没有Cygwin的Windows上的代码。如果我不想在我要针对的Windows box上安装Perl,我可以使用PAR: Packer或PerlApp来生成可执行文件。Python、Ruby和其他语言也应该同样有效。

However, shell scripting isn't all that complicated--at least things that you should be scripting in a shell aren't all that complicated. You can do what you need to do with a fairly shallow level of knowledge.

但是,shell脚本并不是那么复杂——至少在shell中应该编写的东西并不那么复杂。你可以做你需要做的事情,你需要一个相当肤浅的知识水平。

Learn how to read and set variables. How to create and call functions. How to source other files. Learn how flow control works.

学习如何读取和设置变量。如何创建和调用函数。如何找到其他文件。了解流控制是如何工作的。

And most important, learn to read the shell man page. This may sound facetious, but I am 100% serious--don't worry about cramming every detail of shell scripting into your brain, instead learn to find what you need to know in the man page quickly and efficiently. If you find yourself using shell scripting often, the pertinent info will naturally stick in your brain.

最重要的是,学会阅读shell man页面。这听起来可能有点滑稽,但我是100%认真的——不要担心把shell脚本的每个细节都塞进你的脑子里,而是要学会快速有效地在手册页中找到你需要知道的东西。如果您发现自己经常使用shell脚本,那么相关的信息将自然而然地留在您的脑海中。

So, yes, basic shell is worth learning.

所以,是的,基本的外壳是值得学习的。

#16


1  

I'm a perl guy, but the number of the bash (or ksh) functions I use and create on a daily basis is quite significant. For anything involved, I'll write a perl script, but for navigating the directory structure, and specifically for manipulating environment variables bash/ksh/... are indispensable.

我是perl高手,但是我每天使用和创建的bash(或ksh)函数的数量是非常重要的。对于涉及的任何内容,我将编写一个perl脚本,但是用于导航目录结构,特别是用于操作环境变量bash/ksh/…是不可或缺的。

Again, especially for environment variables nothing beats shell, and quite a few programs use environment variables. In Perl, I have to write a bash alias or function that calls the Perl script, which writes out a temporary bash script, which then gets sourced after Perl exits in order to make the change in the same environment I'm launching from.

再说一遍,特别是对于环境变量,没有什么比shell更好的了,很多程序都使用环境变量。在Perl中,我必须编写一个bash别名或函数来调用Perl脚本,该脚本编写一个临时bash脚本,然后在Perl退出后调用该脚本,以便在我正在启动的相同环境中进行更改。

I've done this, especially for heavy-lifting on path variables. But there's no way to do it in just Perl (or python or ruby... or C-code for that matter).

我已经做过了,特别是对于路径变量的重载。但是,仅仅用Perl(或python或ruby……)或者c代码)

#17


1  

As mentioned, the GNU tools are great, and are easiest to use within the shell. It is especially nice if your data is already in a linear or tabular form of plain text. Just as an example, the other day I was able to build a script to create an XHTML word cloud of any text file in 8 lines of Bourne Shell, which is even less powerful (but more widely supported) than Bash.

如前所述,GNU工具非常棒,并且在shell中最容易使用。如果您的数据已经是纯文本的线性或表格形式,那就更棒了。例如,前几天我构建了一个脚本,在Bourne Shell的8行中创建任何文本文件的XHTML字云,它的功能甚至不如Bash强大(但得到了更广泛的支持)。

#18


1  

What I don't get is why people say bash when they mean any bourne-shell compatible shell.

我不明白为什么人们说bash指的是任何与bourne-shell兼容的shell。

When writing shell scripts: always try to use constructs that also work in older bourne shell interpreters as well. It will save you lots of trouble some day.

在编写shell脚本时:总是尝试使用同样适用于旧的bourne shell解释器的构造。总有一天它会为你省去许多麻烦。

And yes, there is plenty of use for shell scripts today, as the shell always exist on all unixes, out of the box, contrary to perl, python, csh, zsh, ksh (possibly?), and so on. Most of the time they only add extra convenience or different syntax for constructs like loops and tests. Some have improved redirection features.

是的,现在shell脚本有很多用途,因为shell始终存在于所有unix上,开箱即用,这与perl、python、csh、zsh、ksh(可能吗?)等不同。大多数情况下,它们只会为诸如循环和测试这样的结构添加额外的便利或不同的语法。有些改进了重定向功能。

Most of the time, I would say that ordinary bourne shell works equally well.

大多数时候,我想说的是,普通的伯恩·贝克尔也同样出色。

Typical pitfall: if ! test $x -eq $y works as expected in bash that has a more clever builtin "if" operator, but the "correct" if test ! $x -eq $y should work in all environments.

典型的陷阱:如果!测试$x -eq $y在bash中如预期的那样工作,它有一个更聪明的builtin“如果”操作符,但是如果测试“正确”的话!$x -eq $y应该适用于所有环境。