2

I have a pretty simple bash script which consists of a bunch of one liners and some simple logic.

Its been recommended that I rip the bash script apart and rewrite it all in python using subprocess. The argument was that we are building a python project, and that this way is more pythonic.

The only advantage that I see is that the logic can be fleshed out with better logging and error handling. On the other hand it seems logical to keep bash things in bash scripts.

Note: I realize this is a best-practice question, I hope this is the right SE channel for these.

1
  • 3
    It's always a bad idea to rewrite something without reason. If it was at least clearer, more efficient, or loads of fun, that would at least be something... Apr 25, 2017 at 12:10

3 Answers 3

3

A few reasons to switch to Python:

  • It won't bother you any longer if someone in your team doesn't know Bash.

  • Maintaining code wouldn't require jumping from Bash to Python code.

  • If logic changes, you will have one reason less to be stuck.

  • Refactoring becomes easier.

  • You can have some relevant metrics about your code. I have never seen any relevant metrics for Bash.

However, keeping the Bash script in place has one major benefit. Just look how ugly does Python code look when you start to do a lot of subprocess calls. Consecutive calls to different processes is better done in Bash, the tool specifically made for that. If, moreover, you're relying on grep and similar tools to work with data, porting such script to Python would be quite difficult and very error prone.

4
  • 3
    If it just uses sub-process calls to bash (as the OP has been told to do), then people still need to know bash, to know what those sub process calls are doing. Apr 25, 2017 at 14:04
  • 1
    @FrustratedWithFormsDesigner: my understanding of the question is radically different. “Its been recommended that I rip the bash script apart and rewrite it all in python” looks more like porting Bash code to Python, rather than just adding a call to the unmodified Bash script from a Python app. Apr 25, 2017 at 19:49
  • "rewrite it all in python using subprocess. " To me, this makes it sounds like bash script ls -l will be replaced with subprocess.call(["ls", "-l"]) which makes the whole exercise seem mostly pointless, if it involves wrapping bash code in python code. Apr 25, 2017 at 19:57
  • 1
    @ArseniMourzenko and @FrustratedWithFormsDesigner. You are both right. ls -l would be ported to os.listdir(), but there were mostly subprocess(["thing", "that", "has", "no", "python", "api"])
    – Roman
    Apr 26, 2017 at 6:17
4

I don't know if it's "pythonic" but it seems like a bad idea to rewrite a bash script in python... with subprocess calls to bash, just because the original script was not in python.

If bash was a good enough tool for the script in the first place, why change it? Especially if you end up with a new script that just wraps calls that were in the original.

If you can come up with a better reason than "it's not python", then go ahead and rewrite.

2

It depends, but in most cases it is best to use python script. All of the below points can be answered using python script, while bash solution might be complicated:

  • Passing parameters to script (bash $1, $2)
  • Input parameters validation and transformation
  • Logging (how to send logs to logging server?)
  • Code reuse (how to reuse code from other bash script?)
  • Collecting metrics and sharing metrics (how much it took for my command to complete?)
  • Testing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.