TPT(Tiny Python Tip): Python for Bash Scripters

by Noah Gift

Are you a sysadmin and know Bash?

If you know Bash, and I mean even as little as being able to throw a few lines together into a script, you can learn Python. I put some example Bash/Python scripts in svn up at Google Code. There are five scripts in total, two are the exact same program written in Bash and Python, and the last script is an example of a complex Unix command line tool that embeds Bash into Python.

http://python4bash.googlecode.com/svn/trunk/


Links:
python4bash
Noah Gift Blog
osxautomation


AddThis Social Bookmark Button

2 Comments

Elias Pipping
2008-01-02 03:50:26
function.sh:
runCommands() in bash doesn't deserve its name... it doesn't *run* anything, it just prints some results.


If you have three commands, each of which takes an hour to complete, with the python script you'll get a report every hour, whereas with the one in bash, the screen will be blank for three hours.


The same applies to sequence.sh, naturally


On a side note:


% count=$((count + 1))


can be written as


% ((count++))


or


% let count++


in bash

Noah Gift
2008-01-02 04:23:15
Elias/Thanks for the comments! One of the reasons I started the project was to get feedback to improve the Bash/Python comparisons. I do agree with the runCommands function, as it is a little weird to compare something Bash does by its very nature, which is run commands, to Python running shell commands


If you, or anyone else for the matter, is interested, it would be fun to collaborate on coming up with the best Bash/Python comparison scripts. I just checked the % let count++ change into the sequence.sh and function.sh. I really like that syntax!


I will have to mull over the a better way to compare a command runner that reflects the nature of Bash and makes sense to Python, or better yet you can do it :)