Ceci est une ancienne révision du document !
Greg's regular Python series will be back next month.
If you are anything like me, after a few years of exposure, the ability to write Bash scripts seemed to get automatically absorbed in my psyche by osmosis. I have also tinkered with Python but find Bash comes so easily to me that I've struggled to justify committing lots of time to learning it to the same level. Even though admittedly, there have been times when Lambda functions or Python dictionaries would have probably meant less effort overall, I still persevered with Bash.
Recently, I was pleasantly surprised to discover someone skilled had created a Bash script/Python script converter called “bash2py” (https://www.swag.uwaterloo.ca/bash2py/index.html).
And, following that piece of wizardry, another clever person wrapped bash2py up into a Docker image for ease of use (https://zwischenzugs.com/2016/08/29/bash-to-python-converter).
Keen to see if it worked, I thought I would try bash2py with a simple script – as we'll see in a moment. At this stage, it's definitely worth pointing out that you will get mixed results with tools like this one. The author of bash2py states that it is intended to only “do the lion share of the translation effort”. And, additionally, if your Bash script contains errors, surprisingly so will the resulting Python script! You have been warned.
That said, for relatively simple jobs, like a Lambda function in AWS for example, this tool should be able to give you the building blocks in Python that could be further developed upon.
Simple Bash Script
Here's a simple script I wrote in order to test out the converter. Its purpose is to employ the very latest technological advances, and… as quickly as possible, it counts to five. The Bash script is shown above.
I have saved the Bash script as a file called “bash.sh”. We’ll save it again in a moment, so stay tuned. To test it works, I ran these commands:
chmod +x bash.sh
./bash.sh
I am a Bash script and I can count…
1 2 3 4 5
I can see the script, counting to five, so we’re all set.
Resulting Python Script
Now that we have a simple Bash script, let's see what bash2py makes of it. We will use the Docker image approach. With Docker already installed, the command to use is this:
docker pull imiell/bash2py
Next we can check how big that image is, with this command:
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE imiell/bash2py latest 09dfc6f8a82e 5 years ago 494MB
As we can see around half a GB of space is taken up by the Docker image.
Let’s see bash2py in action by entering the container after asking Docker to run it:
docker run -ti imiell/bash2py
root@8c3fa65fce45:/opt/bash2py-3.5#
As we can see from the command prompt, we’re inside the container and can execute commands relating to bash2py. Next, we’re going to use the “vi” text editor inside the container and paste the “bash.sh” script into a file of the same name on the container’s filesystem, with this command:
root@8c3fa65fce45:/opt/bash2py-3.5# vi bash.sh
Save that file inside the container by exiting “vi”.
Now, run it too, if you like, with this command (after first making it executable again):
root@8c3fa65fce45:/opt/bash2py-3.5# chmod +x bash.sh
root@8c3fa65fce45:/opt/bash2py-3.5# ./bash.sh
Great, the output is the same as before. We can count to five yet again.
Let’s finally test bash2py with this command:
root@8c3fa65fce45:/opt/bash2py-3.5# ./bash2py bash.sh
The command completes without any output. If you do an “ls” command, you will see that a new file has been created inside the container’s directory called “bash.sh.py”, as so:
ls
MANIFEST README.txt analyzers bash-4.3.30 bash.sh bash.sh.py bash2py bash2py.py bin install source_code.txt tests
If we use Python inside the container first, and execute that Python script, we should be able to see if it runs, as shown with this command:
root@8c3fa65fce45:/opt/bash2py-3.5# python bash.sh.py
I am a Bash script and I can count…
1 2 3 4 5 root@8c3fa65fce45:/opt/bash2py-3.5#
Excellent! We have a working Python script!
What has bash2py changed in our script? In the listing shown above, you can see the contents of our generated Python script.
If you look at Listing Two, most of the work has been done setting up the script so it will run in the environment. At the end of the script, we can see a “for” loop, which is very Bash-like.
The last thing to do is to copy the script outside the container and run it on my laptop, just to be sure.
A reminder that when you type “CTRL-D” inside the container to exit the shell, your container will stop and destroy the data inside it, because of the Docker command we used earlier.
With the Python script copied into my clipboard, I create a new script called “counter.py” and, knowing that my laptop is using Python version 3, then use this command to execute it.
Delta ~ # python3 counter.py
I am a Bash script and I can count…
1 2 3 4 5
As hoped, our simple script runs on my laptop too! Great news.
The End
Obviously you still need some knowledge about Python if you use tools like this.
But bash2py could save you some eyestrain and lots of time under certain circumstances. Be warned you may find, especially for more complex operations, that a fair amount of tweaking is required.
Hopefully, a quick look at this very clever tool has inspired you to try it out for yourself. You never know, it might save the day at some point in the future.