使用 Bash 比 Perl 或 Python 有优势吗?

发布于 2024-11-04 09:18:42 字数 148 浏览 1 评论 0原文

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

是你 2024-11-11 09:18:42

我想到了两个优点:

  • 简单:直接访问所有精彩的 Linux 工具wclscatgrepsed...等。
    为什么不断使用 python 的 subprocess 模块?

  • 我越来越喜欢使用gnu parallel,您可以使用它并行执行 bash 脚本。例如,从手册页中,并行批量创建目录中所有 jpg 的缩略图:

    <代码>ls *.jpg |并行转换 -geometry 120 {}thumb_{}

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

Two advantages come to mind:

  • Simplicity: direct access to all wonderful linux tools wc, ls, cat, grep, sed... etc.
    Why constantly use python's subprocess module?

  • 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_{}

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

煮酒 2024-11-11 09:18:42

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

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

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

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

祝你好运。希望这有帮助。

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:

  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.

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.

Best of luck. Hope this helps.

帅气称霸 2024-11-11 09:18:42

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

对此的讨论:Perl vs Bash

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

A discussion on that: Perl vs Bash

温暖的光 2024-11-11 09:18:42

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

对于调用其他程序的简短、简单的脚本,我将使用 Bash。如果我想保留输出,我很可能会升级到 Python。

例如:

for file in *; do process $file ; done

其中 process 是我想要在每个文件上运行的程序,或者...

while true; do program_with_a_tendency_to_fail ; done

在 Python 或 Perl 中执行这些操作都太过分了。

对于实际编写一个我希望长期维护和使用的程序来说,Bash 很少是适合这项工作的工具。特别是因为大多数现代 Unices 都带有 Perl 和 Python。

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.

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.

For example:

for file in *; do process $file ; done

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

while true; do program_with_a_tendency_to_fail ; done

Doing either of those in Python or Perl is overkill.

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.

过期情话 2024-11-11 09:18:42

与 Python 或 Perl 脚本相比,POSIX shell 脚本最重要的优点是几乎每台 Unix 机器上都可以使用 POSIX shell。 (还有一些任务 shell 脚本恰好更方便一些,但这不是主要问题。)如果可移植性对您来说不是问题,我认为没有必要学习 shell 脚本。

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.

笑着哭最痛 2024-11-11 09:18:42

对于大型项目,请使用 Perl 等语言。

有一些事情您只能在 bash 中执行(例如,更改调用环境(当获取脚本而不是运行脚本时)。此外,shell 脚本编写很常见。学习基础知识并了解如何解决该问题是值得的。 另外,

有时了解 shell 可以拯救你的培根(在无法启动任何新进程的 fork 轰炸系统上,或者如果 /usr/bin 和/或 /usr/local/bin 挂载失败)。

For big projects use a language like 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.

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/bin and or /usr/local/bin fail to mount).

你的往事 2024-11-11 09:18:42

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

而且因为你知道它几乎在任何时候都可以工作......

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.

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

小巷里的女流氓 2024-11-11 09:18:42

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

shell 脚本的缺点就是其他一切。 Shell 脚本语言通常缺乏表达能力、功能和性能。为了确保转义正确,从没有强大字符串处理功能和库的语言中的字符串中破解命令行,会引发安全问题。除非有令人信服的兼容性原因需要使用 shell,否则我个人每次都会选择脚本语言。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文