Friday, November 14, 2008

Overheard

"All you can do with a shell script is make it worse. But since this is Python, you can make it better."

8 comments:

  1. Could you post a little example or a link to some really beautiful Python shell-like scripts?

    I guess invoking external programs, creating a pipeline, quoting parameters, etc. makes Python scripts a little bit ugly, despite of the beauty of the language itself.

    ReplyDelete
  2. I actually tried to make this point in the book I wrote with Jeremy. One of the real problems with shell scripts, is that people don't take them seriously. Pretty soon, you have 2 thousands of something like Perl, but perhaps worse.

    With Python, you can write tests, documentation, and treat these "silly" scripts seriously. Python is absolutely the best language for system administration, and shell-like scripting.

    @andrey take a look at some of these examples:

    http://code.google.com/p/py4sa/source/browse/#svn/trunk/code

    In addition to doing all of the things that say, Bash, does, better, i.e., more readable and testable, you can mix this with the power of the Python language. You can easily create thread pools, use generators, wrap things in command line tools, and more. Python puts the shell to shame!

    ReplyDelete
  3. I wonder what you cannot do in perl that you can do in Python. All talk about writing unmaintainable code in perl is pure myth. Give me python, I can write a real maze of a code and make python look bad to a novice. Languages rarely can enforce disciplined and clean programming. They can just give you constructs to do clean programming!

    ReplyDelete
  4. Andrey Nordin, here's a good script, I think;

    http://www.jperla.com/blog/2008/11/17/a-clean-python-shell-script/

    ReplyDelete
  5. @Noah:

    Take for example a file from your link:
    http://code.google.com/p/py4sa/source/browse/trunk/code/cleansvn.py

    That seems a lot worse than this:
    find ./ -type d -name .svn -exec rm -rf {} \;

    I'm not saying that shell scripts don't get horribly ugly and icky when trying to do something pretty complex, but to say that Python "puts the shell to shame" is far far far from accurate.

    ReplyDelete
  6. I think it wrong and bigoted to think of shell scripting as only a bunch of hacked together scripts that use external programs.
    Bash is a language which simplifies executing external programs. But it is a language all on it's on.
    I have made it my personal crusade to rewrite shell scripts that use external programs to do things that can be written in bash.
    e.g:
    find ./ -type d -name .svn -exec rm -rf {} \;

    can be written in bash as:
    finddo () { [[ "$#" == "3" ]] || { echo usage: $FUNCNAME "directory" "action" "path_filter"; return; }; local d; for d in "$1"/*; do if [ -d "$d" ]; then finddo "$d" "$2" "$3"; [[ "$d" == $3 ]] && $2 $d; fi; done; }

    and executed at the bash prompt or in a script as:
    finddo . "rm -rf" "*.svn"

    This executes the actions depth first to allow for destructive actions.

    ReplyDelete

Note: Only a member of this blog may post a comment.