问题:Shell脚本与Python相比的优势[关闭]

我尝试了几次学习shell(bash)脚本的过程,但是却被语法所取代。然后,我找到了Python,并且能够完成Shell脚本可以在Python中完成的大部分工作。我现在不确定是否应该再花时间在学习shell脚本上了。所以我想问:

与Python相比,shell脚本有哪些优势使其成为必不可少的工具?

我不是专业的系统管理人员,但是我对为家庭用户设置Linux系统感兴趣,因此我认为学习Shell脚本可能变得必要。

I tried to learn shell(bash) scripting few times but was driven away by the syntax. Then I found Python and was able to do most of the things a shell script can do in Python. I am now not sure whether I should invest my time in learning shell scripting anymore. So I want to ask:

What are strengths of shell scripting that make it an indispensable tool as compared to Python?

I am not a system administration by profession, but I am interested in setting up Linux systems for home users, hence I think learning shell scripting can become necessary.


回答 0

  • Shell脚本具有用于I / O重定向的更简单的表示法。
  • 从Shell中的现有程序创建管道更简单。
  • Shell脚本重用了整个程序。
  • Shell是通用的(在Unix之类的东西上)-不一定要安装Python。

“的确,您可以在Python中完成所有可以在Shell中完成的工作;同样,在Python中有些容易实现的事情在外壳中很难实现(就像有些东西在Shell中实现容易但在Python中很难实现一样)。从长远来看,了解两者将是最好的。

  • Shell scripting has simpler notations for I/O redirection.
  • It is simpler to create pipelines out of existing programs in shell.
  • Shell scripting reuses entire programs.
  • Shell is universally available (on anything like Unix) – Python is not necessarily installed.

‘Tis true that you can do everything in Python that you can do in shell; ’tis also true that there are things that are easy in Python that are hard in shell (just as there are things that are easy in shell but hard in Python). Knowing both will be best in the long term.


回答 1

“与Python相比,shell脚本有什么优势使其成为必不可少的工具?”

外壳不是必不可少的。您为什么认为有这么多?bash,tcsh,csh,sh等,等等,

Python 一个外壳。这不是理想的运行脚本,而不是用于运行所有命令的脚本。

Python是所有Linux发行版中的或多或少的标准部分。

更传统的外壳会做很多事情。

  1. 他们有一个方便的用户界面来运行命令。这包括单行命令,shell在其中搜索您的PATH,派生并执行所请求的程序。它还包括管道,序列和并发程序(使用;|&),以及一些重定向(使用><)。

  2. 它们具有运行脚本的简陋的类似于编程语言的功能。这种语言很难使用,效率极低。这种语言的大多数语句都需要分叉一个或多个其他过程,从而浪费时间和内存。

从shell运行程序,将stderr重定向到日志文件,这样的事情就很好了。在外壳中执行该操作。

作为Python脚本,几乎所有其他内容都可以更高效,更清晰地完成。

你们两个都需要。但是,永远不要使用传统的外壳语言编写带有if语句或循环的脚本。

“What are strengths of shell scripting that make it an indispensable tool as compared to Python?”

The shell is not indispensable. Why do you think there are so many? bash, tcsh, csh, sh, etc., etc.,

Python is a shell. Not the one you’d use for running all commands, but for scripting, it’s ideal.

Python is a more-or-less standard part of all Linux distro’s.

The more traditional shells do too many things.

  1. They have a handy user interface for running commands. This includes one-line commands where the shell searches your PATH, forks and execs the requested program. It also includes pipelines, sequences and concurrent programs (using ;, | and &) as well as some redirection (using > and <).

  2. They have a crummy little programming-language-like capability for running scripts. This language is rather hard to use and extremely inefficient. Most statements in this language require forking one or more additional processes, wasting time and memory.

Running programs from the shell, redirecting stderr to a log file and that kind of thing is good. Do that in the shell.

Almost everything else can be done more efficiently and more clearly as a Python script.

You need both. However, you should never write a script with if-statements or loops in a traditional shell language.


回答 2

Shell使普通和简单的动作真正简单了,但代价是使更复杂的事情变得更加复杂。

通常,一个小的Shell脚本会比相应的python程序更短,更简单,但是python程序会趋于优雅地接受修改,而随着代码的添加,shell脚本的可维护性会越来越少。

这样的结果是,为了获得最佳的日常生产力,您需要shell脚本,但是您应该主要将其用于一次性脚本,并在其他地方使用python。

The shell makes common and simple actions really simple, at the expense of making more complex things much much more complex.

Typically, a small shell script will be shorter and simpler than the corresponding python program, but the python program will tend to gracefully accept modifications, whereas the shell script will tend to get less and less maintainable as code is added.

This has the consequence that for optimal day-to-day productivity you need shell-scripting, but you should use it mostly for throwaway scripts, and use python everywhere else.


回答 3

Shell脚本无可比拟,而Python则无能为力。Shell脚本的最大优点是,您使用的命令与使用Shell时使用的命令相同,因此,如果您是大量Shell用户,则Shell脚本有时会成为一种非常快速简便的方法来自动执行Shell工作。

我也发现在shell脚本中处理数据管道比在python中更容易,尽管它在python中绝对可行。

最后,您不必启动其他干扰程序即可运行Shell脚本,从而为您提供了非常小但有时可能在速度和内存使用方面的明显优势。

但是话又说回来,Python脚本更具可维护性,因此我正试图从大型的难看的Shell脚本迁移到Python脚本。使用Python进行异常处理和质量检查也更加容易。

There’s nothing you can do with shell scripts that you can’t do with python. The big advantage of shell scripts is that you use the same commands as you do when you use the shell, so if you’re a heavy shell user, shell scripting will at some point become a very quick and easy way to automate your shell work.

I also find it easier to deal with pipes of data in shell scripts than in python, though it’s absolutely doable from python.

And, finally, you don’t have to fire up an additional interpeter to run the shell scripts, giving you a very small, but sometimes maybe noticeable speed and memory usage advantage.

But then again, Python scripts are a lot more maintainable, I’m trying to migrate from big ugly shell scripts to Python scripts for that very reason. It’s also easier to do exception handling and QA with Python.


回答 4

前面的所有答案都表明,不必学习shell脚本。但是学习永远不是一件坏事。这实际上是个人优先级的问题。别人很难告诉您什么是不值得的。

大多数程序员发现,每次学习新语言都会变得越来越容易。(自然语言也是如此。)并且越早开始越好。

加:学习了一门语言,使您可以从完全的知识和熟悉的位置上摆脱它的局限性。这可能不会让你被解雇,但是可能会从同龄人那里获得啤酒!

one doesn’t have to learn shell scripting, as all the previous answers indicate; but learning is never a bad thing. it’s really a question of personal priorities. it’s very hard for someone else to tell you what is and isn’t worth your time.

most programmers find that learning new languages gets incrementally easier each time. (the same is largely true of natural languages too.) and the earlier you start, the better.

plus: having learned a language enables you to extravagantly diss its limitations from a position of complete knowledge and familiarity. this probably won’t get you laid, but might earn you a beer from your peers!


回答 5

我同意以前的大多数答案。我认为Shell命令最适合执行面向文件系统的任务(复制和移动文件,grep等)。我认为,如果您必须读写文件,那么Shell会更好,因为单个>>file.txt重定向会立即添加到文件中,而不是像file=open('file.txt','a'); file.write()等等。

目前,出于个人用途,我混合了两种方法,分别是创建一个python脚本,然后每次在shell中比在python中更容易执行某个操作时就调用os.system(’command’)或os.popen(’command’)。

I agree with most of the previous answers. I consider shell commands most suited to do filesystem-oriented tasks (copy and move files, grep, etc). Shell is better, in my opinion, if you have to read and write to file, since a single >>file.txt redirection appends to file instantly, instead of needing, say, file=open('file.txt','a'); file.write(), etc.

Currently, for my personal use, I mix both, creating a python script and calling os.system(‘command’) or os.popen(‘command’) every time some action is easier in shell than in python.


回答 6

该外壳随处可见。如果您坚持使用一套相对基本的可移植功能,则您的脚本可以在手机,无线路由器,DVR,上网本,工作站,大型钢铁服务器等上运行。在许多系统上,Python不一定是开箱即用的,根据环境的不同,可能很难安装它。

学习一些shell脚本也可以帮助您学习一些命令行技巧,因为命令行是shell。这对于采取一些相当长且复杂的命令行,并在您意识到您将需要更多的命令行之后将其转换为更通用的脚本也很有用。

该外壳还具有一些非常强大的功能。据我所知,管道是一个非常有趣的控制结构,仅对外壳是本地的。

The shell is available everywhere. If you stick to a relatively basic set of portable functionality, your scripts can run on cell phones, wireless routers, DVRs, netbooks, workstations, big iron servers, and the like. Python is not necessarily included out of the box on lots of systems, and depending on the environment it may be hard to get it installed.

Learning some shell scripting can also help you learn some command line tricks, since the command line is, well, the shell. It’s also good for taking some fairly long and complicated command line, and converting that into a more general script after you realize you’ll need it some more.

The shell also has some pretty powerful features; pipelines are a really interesting control construct that is native only to the shell, as far as I know.


回答 7

选择Python的Shell脚本时要考虑的另一件事是将在目标计算机上运行的Python版本。RHEL5(仅举一例)将存在很长时间。RHEL5停留在Python 2.4中。有很多不错的库,它们依赖于2.4后的Python添加的功能。

Another thing to consider when choosing shell scripts of Python is the Python version that will be running on the target machines. RHEL5 (to name one) is going to be around for a long time. RHEL5 is stuck with Python 2.4. There are a lot of nice libraries that depend on functionality added to Python post-2.4.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。