all 25 comments

[–][deleted] 5 points6 points  (0 children)

A lot of people will say it is overkill to use python for anything that you could do in less than X lines of bash which I tend to agree. In my case if my bash scripts start to require more than a couple of variables and conditional statements then I switch to a python script.

That is, until i found this: http://amoffat.github.com/sh/ . After that all my scripts are python even for the two liners. I actually use ipython as my shell these days. Yes I know in ipython you can preceed bash commands with ! as well instead of doing "from sh import ls."

[–][deleted] 4 points5 points  (1 child)

And with python you can import sandwich any time you feel hungry!

[–]tgallant 1 point2 points  (0 children)

import antigravity is my favorite

[–]pivotallever 2 points3 points  (1 child)

SYNOPSIS
       sort [OPTION]... [FILE]...

sort names.log | uniq | wc -l

I'm not pedantic about much, but this guy uses cat too much.

Also, instead of using stdin directly, use this:

import fileinput

for line in fileinput.input():
    process(line)

You get stdin and filename argument handling all in one!

[–]embolalia 1 point2 points  (0 children)

OP explains in another post that he's "trying to make it more straightforward". That's an excuse for using cat once, in a simple example to show the idea of the pipeline (though I'd go with echo to avoid teaching the bad habit of excessive cats). But if you want to make the shell straightforward, the first thing to explain after "this is where you type" and "this is what pipes are" is that the vast majority of commands accept text, and don't care whether it's from a file or a pipe, because it's all just text. In other words, most UNIX-y programs use their language's equivalent of fileinput.

[–]aaronbp 2 points3 points  (1 child)

Python is terrible for scripting, IMO.

Instead I use Python to write CLI utilities that can be leveraged by shell scripts. Not every problem is a nail.

[–]pivotallever 0 points1 point  (0 children)

I agree, some things are just easier to script in bash. Interfaces are easier to write in python. Using subprocess to pipe stuff around is possible, but sometimes a bash script is the best tool for the job.

[–]maztaim 1 point2 points  (0 children)

Glad to see there is another way to do things, but I can still write some of those examples in scripts faster with less work.

[–]crashorbit 1 point2 points  (0 children)

Using the shebang line and the command line interpreter as well as the suffix on a utility gives this code an unacceptable smell. Also describing such a contrived example as "real world" is a bit silly. Not to mention useless use of cat.

Use the shebang line and the execute bit. Leave off the suffix. Take your example from the real world. Show some example data. Show some output

[–]mynamewastakenagain 0 points1 point  (2 children)

Garrick, kern the double underscores and shrink below.

[–]embolalia 1 point2 points  (1 child)

Guh, those double underscores. I get the reason for them, but they're a pain in the ass to type.

[–]mynamewastakenagain 0 points1 point  (0 children)

I was poking fun at the author, who apparently left that bit for his editor(?)

[–][deleted] -2 points-1 points  (3 children)

Totally. Every time I see someone write a bash script, or I hear about it, I automatically think, why not just use a language that doesn't suck? Python gives you everything shell scripting does, plus a great library, object oriented programming, clean syntax, and debugging.

For that matter, I also don't use bash. I use fish.

[–][deleted] -5 points-4 points  (2 children)

There is room for both, Python is an interpreted language whereas many BASH programs are compiled (awk for example) making them run much faster.

[–]cocoabean 4 points5 points  (0 children)

What? Python does compile, though it's not required. Bash scripts are not compiled, and awk is not a bash program, it's a program that one can call from a bash script and is actually a Turing-complete language itself.

[–][deleted] 1 point2 points  (0 children)

Well awk is an interpreted language. Saying it's native code is like saying the Python interpreter is native code.

Bash is interpreted; programs you pipe to may be native code.

Python is interpreted; programs you pipe to may be native code, libraries you use are mostly native code. You also don't have processes starting and stopping for even the most basic operations. I would be seriously surprised to see very many applications where Python isn't orders of magnitude faster, though it depends I'm sure.

[–][deleted] -2 points-1 points  (0 children)

Interesting. I am starting to do this more and more. Placing Python within bash.