python along or bash combined with python (for manipulating files)

Falcolas garrickp at gmail.com
Wed Oct 14 13:42:25 EDT 2009


On Oct 13, 10:18 pm, TerryP <bigboss1... at gmail.com> wrote:
> On Oct 14, 2:13 am, Peng Yu <pengyu... at gmail.com> wrote:
>
> > Bash is easy to use on manipulating files and directories (like change
> > name or create links, etc) and on calling external programs. For
> > simple functions, bash along is enough. However, bash does not support
> > the complex functions. Python has a richer library that could provide
> > support for complex functions (such compute the relative path between
> > two paths).
>
> > I'm wondering for a task that can not be done with bash along whether
> > it would be better to do in pure python or with a mix of both python
> > and bash. What I care is mostly coding speed and a little bit
> > maintainability (but not much). Can somebody provide some experience
> > on when to combine python and bash and when to use pure python?
>
> bash can **not** manipulate files and directories beyond things like
> the '>' and '>>' I/O redirections, and some minor loading/saving of
> state data from/to files (command history, directory stack, etc). Most
> of what you refer to are **separate operating system specific
> programs** and have absolutely nothing to do with the shell.
>
> Very sophisticated scripts are possible using bash and ksh, there is
> even a form of ksh that has tk capabilities! (tksh). The Python and
> Bourne-derived languages are however fundamentally different
> creatures, and use very different data models. You should **not**
> write Python (or Perl) scripts as if they were shell scripts -- doing
> so is very bad practice. When you want a shell script, write a shell
> script. When you write a Python script, write a Python script. It
> really is that simple.
>
> As a rule of thumb, when you have need of data structures beyond what
> scalar strings and very simple word lists can provide -- you should
> use Python. bash and ksh provide support for arrays, and ksh even has
> dictionaries! (Hashes in Perl speak.) That makes programming in bash/
> ksh more robust then pure sh, but also less portable. The best time to
> use bash is when you require bash specific features, other wise don't
> use bash. The same can be said for ksh.
>
> When the words array, dictionary, class, object, and/or using multiple
> source files comes to mind when implementing a program - you probably
> want to use Python, Perl, Ruby, or some other general programming
> language, not a shell scripting language like bash.
>
> You should be cautious to avoid mixing bash and Python code in one
> file.
>
> If maintainability is not a factor in what you are writing, then you
> should probably not be writing code in any language unless it is the
> language of Mathematics (and even then, maintainability is a wise
> consideration).
>
> --
>   TerryP.
> Just Another Programmer.

With all of Terry's admonitions in mind, Python scripts do integrate
very well as a individual tool within a shell toolchain. With the
multiple command line parsers and the ease of reading stdin and
writing to stdout, it's fairly trivial to make a script which
integrates cleanly into a bash script (or oneliner). It's trivial to
implement a script which will either work with files, or work with
stdin/stdout.

Garrick



More information about the Python-list mailing list