Automatically converting (most of?) n bash scripts to Python?

Hello people.

I’ve been writing a bunch of scripts, sometimes in bash, sometimes in Python, to automate the mundane parts of my job.

My management has decided they don’t want the bash anymore, so I’ve been asked to move in the direction of Python. That’s kind of cool, really.

I’m looking for a tool that can automate some or all of that process.

I’ve had mixed success with AI-based programming language conversion, and in a 1-page google search, I don’t see a good one for bash → Python.

I tried GitHub - clarity20/bash2py: Bash-to-python transpiler https://www.swag.uwaterloo.ca/bash2py/index.html - which apparently modified an old version of bash to add outputting roughly equivalent Python. Maybe it worked well at one time, I don’t know. But within the last week or two I tried it, and found that it segfaulted a lot.

I also looked at bash2python/bash2python.sh at master · syuanca/bash2python · GitHub - but it seems almost like an April Fool’s joke. It just outputs a bash script and runs it with the subprocess module.

Does anyone know of any (other) bash → Python translation software?

Thanks!

As far as I know there isn’t a generalized bash to python converter out there. In general there are going to be small intricacies that make direct translation not straight-forward.

It’s kind of odd for management to pick the language for code, typically that should be up to the people writing/maintaining the code to pick the best tool for the job.

If it’s a real requirement, I recommend moving over bits of the code at a time. Start with some functions and libraries, and expand case by case. Be sure to write/run unit tests to ensure the new code works as expected.

1 Like

I would push back on management. As much as I loved Python, there are things that BASH is better at. And taking working scripts and porting them with no valid technical reason seems very wasteful to me.

2 Likes

Someone by the name of chubot does a lot of work in this area at: https://www.oilshell.org/. Lots of interesting articles on their blog. Not sure there is a ready-made solution that fits your needs but maybe some inspiration at least.

Bash scripts using source or . can change the caller’s environment. I can’t think of an easy way to do that in Python. The obvious way doesn’t work (as the docs explain).

>python3 -c "import os; os.environ['FOO']  = 'BAR'"
>echo $FOO
>echo "export FOO=BAR" > set_foo
>source set_foo
>echo $FOO
BAR

I wrote my own parser for about 5 classes of .bat commands in some test files. My goal was to keep the original .bat files as the single source of truth for the legacy tests.

So if they’re not run using source or ., translating Bash to Python’s feasible, but a lot of work, even just for line by line commands. As soon as loops, conditionals, variables and other internal state, or the dreaded gotos are required, I can envisage a lot of complexity.

It depends how many there are, but a rewrite of the bash scripts would be far easier, in my opinion. Scripts that run at that level could be doing anything, so should be a concern of the programmer.

Most automated tools are fantastic when they work, when your input data falls within the scope they were designed to support. As soon as there’s a bug or something outside of that scope is required however, they’re equally fantastic at creating work.

I completely agree with Dan. Especially in time of various containers there is non-neglible value in shell scripts that they require zero dependcies. Besides, Don’t fix it when it works™.

1 Like