The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Can Python obsolete bash scripting?

Is it possible to use Python to eliminate the need for bash scripting? I haven't been able to find python Packages that would let you do deep system stuff (e.g. scheduling cron jobs) or even simple but system-specific stuff like searching a list of current processes e.g.
ps -A |grep some_daemon > some_file.log

Am I just not looking in the right place? Or do you still have to do your gluing with bash even though you do your thinking with Python or C?
John Foxx
Sunday, May 29, 2005
Cron: "If you just want to know how to invoke crontab(1) from a Python script, look up os.popen() and friends in the standard library documentation.  As for getting the cron daemon to reload the changed crontab, that's a nop in the crons I've seen; they check for changes every minute anyway."

1. "Take a look at os module. There are some methods like geteuid() and other."
Sunday, May 29, 2005
Thanks Fred, that's what I was looking for. One question, though: is there a consensus among people who are good at this sort of thing re. whether Python has enough stuff to make bash scripting completely unnecessary? (As you probably guessed, I don't have much love for bash scripts & would prefer to do everything from Python.)
John Foxx
Sunday, May 29, 2005
as far as I`m concerned, bash (or any other shell for that mather) scripting was obsoleted by perl about 10 years ago.

Sunday, May 29, 2005
John Foxx > One question, though: is there a consensus among people who are good at this sort of thing re. whether Python has enough stuff to make bash scripting completely unnecessary?

What's the goal? You teach Linux, and need to decide whether to teach kids about shell scripting, or just tell them about richer scripting tools like Python, Perl, or Ruby? Since bash is the standard scripting language in Linux, sys admins will need to know bash et al. for years to come, no matter how clunky they are compared to those other languages.

OTOH, if the goal is simply to choose a command-line scripting language to teach basic programming in Linux, yes, more recent languages offer much more features and improved productivity.
Monday, May 30, 2005
If you're using Python 2.4, you want to look at the subprocess module instead of os.popen and friends -- it's much easier to use. If you're using an older version, the module from 2.4 should still work on it.

Your example of 'ps -A | grep some_daemon > some_file.log' would translate to

import subprocess as S

p1 = S.Popen(['ps','-A'], stdout=S.PIPE)
fo = open('some_file.log', 'w')
p2 = S.Popen(['grep', 'some_daemon'], stdin=p1.stdout,

... although I'd do the grep within python itself:

p1 = S.Popen(['ps', '-A'], stdout=S.PIPE)
fo = open('some_file.log', 'w')
for line in p1.stdout:
    if line.find('some_daemon') != -1:
scruffie Send private email
Monday, May 30, 2005
ps -A | grep some_daemon > some_file.log'

Seems a bit easier.  +1 to Perl replacing Bash a while ago.
Jared M.
Tuesday, May 31, 2005
Actually, I've never done any bash scripting. I'm reasonably comfortable navigating around in a bash shell and executing simple commands. But for any real scripting, I've always used Perl. Does the trick for me.
BenjiSmith Send private email
Tuesday, May 31, 2005
I have given up on bash. It doesn't have built-in lists which makes simple things very difficult, eg:

backup_files="dir1 dir2 dir3 dir with spaces 4"

tar -czf $target_file.gz $backup_files
(The real script was more complicated than this, but you get the idea)

I tried every possible method I could think of (or could find in the documentation, or google) to escape or quote the spaces in $backup_files, but nothing worked. In the end I gave up and rewrote the script in Python.

I'd be interested if anyone could make that bash snippet work.

Saturday, June 04, 2005
Quote and use eval:
backup_files="dir1 dir2 dir3 \"dir with spaces 4\""
eval tar czf $target_file.gz $backup_files

The answer to the most complex avatar of this question:
for i in `complex command that generates filenames with spaces`
  IFS=' '
Kartik Agaram
Monday, June 13, 2005
Thanks for that. It's a bit of a hack, but it works.

Using Windows and Unix together leads to all sorts of little frustrations. How I wish I had a Mac.

Tuesday, June 14, 2005

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz