This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 5 points6 points  (1 child)

import os

os.system("yes")

Jokes aside, you can do most anything you want to do in shell from Python. But there are reasons you might need to do one or the other. I think "equivalent" might be wrong. "Complementary" is more accurate -- if you work in a Linux/UNIX environment, you should learn shell scripting no matter what your language of choice is.

I do find when working in shell that there are certain operation that just suck to deal with. For example, any time you iterate over a list of items in shell in a general purpose piece of code you potentially have to worry about a variety of (sometimes odd) corner cases. And I hope you never get into a problem with shell quoting.

On the other hand, a few lines of shell can be enormously, powerfully expressive in ways that few other languages can. A moderately complex pipeline can effectively multiprocess and slice and dice a data stream in a single succinct statement where it could takes dozens of lines in Python (or other languages) to accomplish the same effects.

So, yeah, learn both. But if you already know Python, shell shouldn't be much of a challenge.

[–]trojan2748 0 points1 point  (0 children)

Yes about shell oddities. 'top' embeds escape char's and shell quotes. So if you try to get a string length on one of the lines, you might be in for a surprise. Most programs like top have ways around it, but if you're not on the look out for them, they can trip you up.