views:

140

answers:

3

I need to write a script to do the following:

  1. Monitor a queuing system, which is accessible by shell commands.
  2. Create directories from templates using a mix of inline text editing, cp/mv, command line scripts, and compiled c++ programs.
  3. Check for error conditions.
  4. Write files on error conditions.

Note: 2D arrays would be mildly useful to my program, but I'm currently making due with several 1D arrays (due to the limitations of Bash script's arrays).

Those tasks all seems somewhat "shell heavy" in so much as it can easily be implemented with a bunch of shell commands, so I thought Bash scripting would be a natural way to go. My results thus far have been good, but before I begin refactoring and finalizing my code, I was wondering whether it'd be better to port it to Python. I've read in numerous places that Python is "superior" to bash. I've done a bit of Python scripting and as far as I can tell, that's because it has more built in libraries and has support for object-oriented programming. However, all the scripts I've seen using shell commands, such as this one: http://magazine.redhat.com/2008/02/07/python-for-bash-scripters-a-well-kept-secret/

Implement obnoxious syntax, like having to define commands as variables, like so:

#You could add another bash command here
#HOLDING_SPOT="""fake_command"""

#Determines Home Directory Usage in Gigs
HOMEDIR_USAGE = """
du -sh $HOME | cut -f1
"""

#Determines IP Address
IPADDR = """
/sbin/ifconfig -a | awk '/(cast)/ { print $2 }' | cut -d':' -f2 | head -1
"""

...and requiring wrapper functions and other fun.

Am I just being silly or does that seem less intuitive? Is there a speed advantage for use with Python that would outweigh the simplicity advantage of Bash when it comes to shell script commands? Or is the syntax of bash (no 2D arrays, brace/parentheses intricacies) reason to jump to Python?

+5  A: 

If you can't find a reason to switch it's probably because there's no reason.

I started to switch python because some of my scripts require some templating process that was easier to do in other than shell script.

There were also another scripts wich required configuration file parsing or parameter parsing that make me learn a bit more of python, and finally another ones which deal with trac (coded in python) who made me switch to python.

But if you could outline your scripts in bash fast and cleanly without require another tool, remain in bash, it is a great tool.

theist
At my place of employ there is a tool written in Python that pretty much exclusively consists of shell commands. Python was almost certainly the wrong choice for this, but the truth remains that you can (often) do the bits that work best in each respective language, and then switch languages when the problem better suits a different one.
dash-tom-bang
+2  A: 

Bash is good right at the actual interface with the system, because it's so easy to run external programs, and for processing data that comes in streams. Python is good at work with less surface area, involving more looping, logic, data structures, and so on. Horses for courses.

It's hard to advise you which to use, or how to use both, without knowing details of your problem. But from the sound of it, this is something i'd do entirely in shell script. I'd find a way of doing it without arrays, though; the key is to stop thinking in C++ and start thinking in bash.

Tom Anderson
I'm not really thinking in C/C++. I'm just saying that certain variables I have could be grouped together. For example, for a number of names BC0, BC1... I create 9 directories each (so 10 names, I would create 90 dirs). In each of these dirs I need to substitute three unique numbers. So if bash supported 3D arrays, I could store all the #s in one array. Thus the reduction would be from (# names * # of unique numeric entries need) to 1. That's a reduction in my current implementation from 54 arrays, down to 1. Surely you see how that would be helpful?
Jason R. Mick
As to your abstraction, my program has more surface area (lots of shell-type manipulations and little looping/logic). Thus it seems Bash scripting may be a better answer. I tried to do a good job listing the details of my program (lots of shell commands, description of its functions) without putting a precise code listing. Any specific suggestions in that respect for next time?
Jason R. Mick
I think you described it as well as you could without going into details. Some questions can't be answered conclusively without going into details, sadly. My comment about arrays may be more of a style preference of my own than good general advice; i tend to approach problems like this by turning them into loops which only need to keep track of one thing at a time inside the loop body. Might not be possible in your case, of course.
Tom Anderson
A: 

It really depends on what you are trying to do, but here are some general differences. Shells, including bash, want to treat all variables as strings and to access variable values through simple textual substitution. Sometimes this is convenient; sometimes not so much. In shell scripts you tend to manipulate data by piping it through various utilities. Sometimes this is convenient; sometimes not so much. Python has very good string manipulation, so if you are doing a lot of text file munging it can be faster (both to write and to run) to do it in Python rather than figuring out what sed or awk incantation you want. That is another thing -- many of the tools you will use in shell scripts have so many options that they are essentially mini-languages of their own, some of which are terse to the point of being obtuse. Python is much more readable than shell scripts that invoke a lot of these mini-languages since it is consistent syntactically. Some projects lend themselves to an object-oriented approach, which Python supports handily, while bash... not so much. Python also supports some functional techniques, which are handy for some projects, while bash... you get the picture. In short, a shell's scripting language is intended to be glue for tying together various single-function utilities, with some conveniences to make that easier, while Python is a robust programming language in which you can build complete GUI or Web applications if you like.

I personally also find the overall syntax of Python much nicer than a shell scripting language; my programs tend to do what I want without a lot of fiddling, and when they don't it's usually because of a logic error rather than an error translating what I want into the language. Python is nearly unique in this regard, though I understand Ruby is good as well.

If you are familiar with and like shell scripting, Perl might be an easier step up since it was originally intended to replace shell scripts (and sed/awk).

kindall