Advantages of Bash Script v. Python Script for Shell-Command-Heavy Utility

I need to write a script to do the following:

  • Track the queue system available to shell commands.
  • Creating directories from templates using a combination of built-in text editing, cp / mv, command line scripts and compiled C ++ programs.
  • Check error conditions.
  • Writing files for errors.

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

All of these tasks seem somewhat "heavy" because they can easily be implemented using a set of shell commands, so I thought the Bash script would be a natural way. So far, my results have been good, but before starting refactoring and completing my code, I was wondering if I should port it to Python. I read in many places that Python is "superior" to bash. I made several Python scripts, and as far as I can tell, this is because it has more built-in libraries and supports object-oriented programming. However, all the scripts that I saw with shell commands, such as: http://magazine.redhat.com/2008/02/07/python-for-bash-scripters-a-well-kept-secret/

Introduce obscene syntax, for example, define commands as variables, for example:

#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 wrappers and other fun features.

Am I just stupid, or does it seem less intuitive? Is there a speed advantage for use with Python that outweighs the advantage of Bash simplicity when it comes to shell script commands? Or is it Bash syntax (without 2D arrays, complexities with brackets / brackets) to switch to Python?

+6
performance python bash shell refactoring
source share
3 answers

If you cannot find a reason to switch it, probably because there is no reason.

I started switching python because some of my scripts required some template process which was easier to do than shell script.

There were also other scripts that required parsing the parameters or parsing the parameters, which made me learn a little more python, and finally others that related to trac (encoded in python), which made me switch to python.

But if you could quickly and cleanly draw your scripts in bash without requiring another tool, stay in bash, this is a great tool.

+7
source share

Bash is well suited to the actual interface with the system, as it easily launches external programs and processes data coming into streams. Python is good at working with less surface area, with more cycles, logic, data structures, etc. Horses for courses.

It is hard to advise you to use, or how to use both, without knowing the details of your problem. But because of the sound, this is what I would do completely in a shell script. I would find a way to do this without arrays; the key is to stop thinking in C ++ and start thinking in bash.

+3
source share

It really depends on what you are trying to do, but here are some common differences.

Shells, including bash, want to treat all variables as strings and get the values ​​of variables through simple textual substitution. Sometimes it’s convenient; sometimes not so much.

In shell scripts, you try to manipulate data by passing them through various utilities. Sometimes it’s convenient; sometimes not so much.

Python has very good string manipulations, so if you work a lot with a text file, it can be faster (both for writing and running) to do this in Python, and not to determine which sed or awk spell Do you want to.

Another thing is that many of the tools that you will use in shell scripts have so many options that they are essentially mini-languages, some of which are short to be dumb. Python can be much more readable than shell scripts that invoke many of these mini-languages ​​because they are syntactically consistent.

Some projects lend themselves to an object-oriented approach that supports Python, and bash ... not so much. Python also supports some functional methods that are convenient for some projects, and bash ... you get an image.

In short, the shell scripting language is designed to glue various single-functional utilities with some amenities to make it easier, while Python is a reliable programming language in which you can create a complete graphical interface or web applications if you like.

I personally also find that the general Python syntax is much better than the shell scripting language; my programs, as a rule, do what I want without a lot of exercises, and when they do not, usually because of a logical error, and not an error that translates what I want into the language. Python is almost unique to me in this regard, although I have heard Ruby programmers say similar things about their favorite language.

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

+3
source share

All Articles