Linux 与 Perl 或 Python 相比,使用 Bash 有什么优势吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5858877/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:53:51  来源:igfitidea点击:

Is there an advantage to using Bash over Perl or Python?

pythonlinuxperlbashscripting

提问by ManAmongHippos

Hey I've been using Linux for a while and thought it was time to finally dive into shell scripting.

嘿,我已经使用 Linux 一段时间了,我认为是时候深入研究 shell 脚本了。

The problem is I've failed to find any significant advantage of using Bash over something like Perl or Python. Are there any performance or power differences between the two? I'd figure Python/Perl would be more well suited as far as power and efficiency goes.

问题是我没有发现使用 Bash 比 Perl 或 Python 有任何显着优势。两者之间是否有任何性能或功率差异?我认为就功率和效率而言,Python/Perl 会更合适。

回答by Mario Peshev

Perl scripts are usually (if not 100% of the times) faster than bash.

Perl 脚本通常(如果不是 100%)比 bash 快。

A discussion on that: Perl vs Bash

对此的讨论:Perl 与 Bash

回答by Sven Marnach

The most important advantage of POSIX shell scripts over Python or Perl scripts is that a POSIX shell is available on virtually every Unix machine. (There are also a few tasks shell scripts happen to be slightly more convenient for, but that's not a major issue.) If the portability is not an issue for you, I don't see much need to learn shell scripting.

POSIX shell 脚本相对于 Python 或 Perl 脚本的最重要优势是,POSIX shell 几乎可以在每台 Unix 机器上使用。(还有一些任务 shell 脚本碰巧稍微方便一些,但这不是主要问题。)如果可移植性对您来说不是问题,我认为没有太多需要学习 shell 脚本。

回答by drysdam

If you want to execute programs installed on the machine, nothing beats bash. You can always make a system call from Perl or Python, but I find it to be a hassle to read return values, etc.

如果你想执行安装在机器上的程序,没有什么比 bash 更好的了。你总是可以从 Perl 或 Python 进行系统调用,但我发现读取返回值等很麻烦。

And since you know it will work pretty much anywhere throughout all of of time...

而且因为你知道它几乎可以在任何地方在任何时间工作......

回答by nmichaels

The advantage is that it's right there. Unless you use Python (or Perl) as your shell, writing a script to do a simple loop is a bunch of extra work.

优点是它就在那里。除非你使用 Python(或 Perl)作为你的 shell,否则编写一个脚本来执行一个简单的循环是一堆额外的工作。

For short, simple scripts that call other programs, I'll use Bash. If I want to keep the output, odds are good that I'll trade up to Python.

对于调用其他程序的简单脚本,我将使用 Bash。如果我想保留输出,我会换用 Python 的可能性很大。

For example:

例如:

for file in *; do process $file ; done

where processis a program I want to run on each file, or...

process我想在每个文件上运行的程序在哪里,或者...

while true; do program_with_a_tendency_to_fail ; done

Doing either of those in Python or Perl is overkill.

在 Python 或 Perl 中执行其中任何一个都太过分了。

For actually writing a program that I expect to maintain and use over time, Bash is rarely the right tool for the job. Particularly since most modern Unices come with both Perl and Python.

对于实际编写我希望随着时间的推移维护和使用的程序,Bash 很少是适合这项工作的工具。特别是因为大多数现代 Unices 都带有 Perl 和 Python。

回答by bobince

The advantage of shell scripting is that it's globally present on *ix boxes, and has a relatively stable core set of features you can rely on to run everywhere. With Perl and Python you have to worry about whether they're available and if so what version, as there have been significant syntactical incompatibilities throughout their lifespans. (Especially if you include Python 3 and Perl 6.)

shell 脚本的优点是它在 *ix 机器上全局存在,并且具有相对稳定的核心功能集,您可以依赖它在任何地方运行。使用 Perl 和 Python,您必须担心它们是否可用以及如果可用是什么版本,因为在它们的整个生命周期中存在明显的语法不兼容。(特别是如果您包含 Python 3 和 Perl 6。)

The disadvantage of shell scripting is everything else. Shell scripting languages are typically lacking in expressiveness, functionality and performance. And hacking command lines together from strings in a language without strong string processing features and libraries, to ensure the escaping is correct, invites security problems. Unless there's a compelling compatibility reason you need to go with shell, I would personally plump for a scripting language every time.

shell 脚本的缺点是其他一切。Shell 脚本语言通常缺乏表现力、功能和性能。并且在没有强大的字符串处理功能和库的语言中将命令行黑客攻击到一起,以确保转义是正确的,这会引发安全问题。除非有令人信服的兼容性原因需要使用 shell,否则我个人每次都会选择一种脚本语言。

回答by Sebastian

Two advantages come to mind:

想到两个优点:

  • Simplicity:direct access to all wonderful linux tools wc, ls, cat, grep, sed... etc. Why constantly use python's subprocessmodule?
  • I'm increasingly fond of using gnu parallel, with which you can execute your bash scripts in parallel. E.g. from the man page, batch create thumbs of all jpgs in directory in parallel:

    ls *.jpg | parallel convert -geometry 120 {} thumb_{}

  • 简单性:直接访问所有优秀的 linux 工具wc, ls, cat, grep, sed... 等等。为什么不断使用 python 的subprocess模块?
  • 我越来越喜欢使用gnu parallel,您可以使用它并行执行 bash 脚本。例如,从手册页中,并行批量创建目录中所有 jpg 的缩略图:

    ls *.jpg | parallel convert -geometry 120 {} thumb_{}

By the way, I usually have some python calls in my bash scripts (e.g. for plotting). Use whatever is best for the task!

顺便说一句,我通常在我的 bash 脚本中有一些 python 调用(例如用于绘图)。使用最适合任务的任何东西!

回答by jonesy

bash isn't a language so much as a command interpreter that's been hacked to death to allow for things that make it look like a scripting language. It's great for the simplest 1-5 line one-off tasks, but things that are dead simple in Perl or Python like array manipulation are horribly ugly in bash. I also find that bash tends not to pass two critical rules of thumb:

bash 与其说是一种语言,还不如说是一种命令解释器,它被砍死以允许使其看起来像脚本语言的东西。它非常适合最简单的 1-5 行一次性任务,但在 Perl 或 Python 中非常简单的事情(如数组操作)在 bash 中非常丑陋。我还发现 bash 往往不会通过两个关键的经验法则:

  1. The 6-month rule, which says you should be able to easily discern the purpose and basic mechanics of a script you wrote but haven't looked at in 6 months.

  2. The 'WTF per minute' rule. Everyone has their limit, and mine is pretty small. Once I get to 3 WTFs/min, I'm looking elsewhere.

  1. 6 个月规则,它表示您应该能够轻松辨别您编写但在 6 个月内没有看过的脚本的目的和基本机制。

  2. “每分钟 WTF”规则。每个人都有自己的极限,我的很小。一旦达到 3 WTFs/min,我就会寻找其他地方。

As for 'shelling out' in scripting languages like Perl and Python, I find that I almost never need to do this, fwiw (disclaimer: I code almost 100% in Python). The Python os and shutil modules have most of what I need most of the time, and there are built-in modules for handling tarfiles, gzip files, zip files, etc. There's a glob module, an fnmatch module... there's a lot of stuff there. If you come across something you need to parallelize, then indent your code a level, put it in a 'run()' method, put that in a class that extends either threading.Thread or multiprocessing.Process, instantiate as many of those as you want, calling 'start()' on each one. Less than 5 minutes to get parallel execution generally.

至于在像 Perl 和 Python 这样的脚本语言中“炮轰”,我发现我几乎从来不需要这样做,fwiw(免责声明:我几乎 100% 用 Python 编写代码)。Python os 和 shutil 模块有我大部分时间需要的大部分内容,并且有用于处理 tarfiles、gzip 文件、zip 文件等的内置模块。有一个 glob 模块,一个 fnmatch 模块......有很多那里的东西。如果您遇到需要并行化的内容,则将代码缩进一个级别,将其放入“run()”方法中,将其放入扩展 threading.Thread 或 multiprocessing.Process 的类中,实例化其中多个你想要,在每一个上调用'start()'。一般不到 5 分钟即可获得并行执行。

Best of luck. Hope this helps.

祝你好运。希望这可以帮助。

回答by daotoad

For big projects use a language like Perl.

对于大型项目,请使用 Perl 之类的语言。

There are a few things you can only do in bash (for example, alter the calling environment (when a script is sourced rather than run). Also, shell scripting is commonplace. It is worthwhile to learn the basics and learn your way around the available docs.

有一些事情你只能在 bash 中做(例如,改变调用环境(当脚本是源代码而不是运行时)。此外,shell 脚本是司空见惯的。学习基础知识和学习方法是值得的可用的文档。

Plus there are times when knowing a shell well can save your bacon (on a fork-bombed system where you can't start any new processes, or if /usr/binand or /usr/local/binfail to mount).

此外,有时了解 shell 可以节省您的培根(在无法启动任何新进程的 fork-bombed 系统上,或者如果挂载/usr/bin/usr/local/bin挂载失败)。