Shell 脚本与 Python 相比的优势

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

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

我不是一个专业的系统管理员,但是我对为家庭用户设置 Linux 系统感兴趣,因此我认为学习 shell 脚本编程是必要的。

66050 次浏览
  • 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.

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.

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.

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

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.

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!

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.

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.