Site iconJava PDF Blog

Python – a Multiplatform Alternative to Bash/Batch Scripts?

Here at IDRSolutions, we have multiple scripts for running our different tests, which automate all the tedious setup and whatnot. This lets us run most tests in the background, separate from the IDE (Which then allows for some neat multitasking). While we do have a server periodically running the tests for us, we need to be able to run these locally on demand to assist with finding and fixing bugs, and to make sure our last set of commits won’t set the build server on fire.

Because some of us develop on Windows and others on Mac (Apparently there’s a Linux box or 2 lying around as well), we have 2 scripts for each set of tests: a Batch script for Windows, and a Bash script for Linux and Mac nerds. The issue here is that if we make a change to 1 script, we have to mirror the change in the other as well. Maintaining both scripts is feasible, but can be a pain and become time consuming, especially with the longer ones. What we need is a set of scripts that can run on any OS – enter Python.

So how does Python fit in?

First and foremost: Python is multiplatform. Regardless of OS, we need only 1 script. Fantastic.

Of course, apart from ticking the one and only box that required ticking, I found other benefits in switching to Python:

Python won’t replace everything – like Bash/Batch it’s being used as glue between programs. For example, we’ll still use Ant to build and JUnit to test, just now we’ll have Python managing it all for us instead of Bash/Batch.

Although Bash/Batch scripts are very powerful tools when it comes to smaller tasks, some languages are better suited to longer, more complex programs – every language has its pros and cons depending on what you want to do. Disclaimer: The image is an exaggeration.

But doesn’t Windows 10 have Bash support now?

I was initially excited at the thought of a quick and easy solution, and decided to play around with Bash on Ubuntu on Windows.

However.

I discovered that Bash isn’t actually fully integrated into Windows the way the command line is – you can’t launch any Windows programs, or run any Windows commands from within the Bash shell or a script. Although you can access the same files, Bash on Ubuntu on Windows is separate i.e. any programs you install in the Bash shell cannot be used/accessed/interacted with outside of the shell, and vice-versa. It didn’t help that setting up Bash on Windows was rather awkward (compared to installing Python, for example) having to go through multiple menus and hidden settings to enable the Windows Subsystem for Linux beta. The final nail in the coffin was the requirement of Windows 10 (with the Anniversary Update), which not every Windows machine in the office uses.

Unfortunately this meant that Bash was out of the question.

So why did we pick Python? What was wrong with Perl or Ruby? Couldn’t we have just cut out having to spend time learning another language and just use Java?

Well, that’s a tale for another time.

Have you had any experience using Python/Perl/Ruby/etc. to replace Bash/Batch scripts? Let us know in the comments below.