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

P

Peng Yu

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?
 
S

samwyse

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?

Scripting languages try to optimize gluing disparate programs together
to accomplish a task; bash excels at this. Programing languages try
to optimize finding the solution to a problem; Python excels at this.

Generally, I try to stick to one language per problem, be it bash, C+
+, Java, Perl or Python. Bash scripts translate easily into the
others, so you don't lose much time if you decide you started with the
wrong language.

Countering that, I also maintain a "toolbox" of programs that I can
call upon when needed. In those cases, I don't hesitate to call a
program that I've written in any language from a bash script.

BTW, I actually prefer ksh to bash, but YMMV.
 
T

TerryP

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).
 
F

Falcolas

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).

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
 
A

Aahz

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.

Oh, well, I guess I follow bad practice a lot. Shame on me.

(That is, I disagree that it's bad practice to use Python as if it were
a straight scripting language, os.system() and all. I prefer using
Python because it makes it easy to upgrade scripts as needed.)
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"To me vi is Zen. To use vi is to practice zen. Every command is a
koan. Profound to the user, unintelligible to the uninitiated. You
discover truth everytime you use it." (e-mail address removed)
 
P

Peng Yu

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.

Do you know what are bash and ksh specific features? Is there a thing
that bash/ksh can do but python can not do?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top