So I wanted to make a script that could be ran on both windows and unix based systems to help with distributing things on multiple platforms, all in 1 script without any changes to said script.
To do this, I first tried to use the “ErrorActionPreference” enviromental variable.
1 | #!/bin/bash |
At first, this seems to work, however, if we look at any command that is shared between the operating systems, we can see this fails, such as the following:
1 | #!/bin/bash |
This, obviously, leads to powershell outputting that echo.
My first thought to fix this, would be to simply modify the third and forth line to cal && echo "Unix!"
, but, that seems to throw an error not caught by the powershell enviroment variable we set earlier in some versions. To fix this, i need a universal logical AND.
This, however, proved to be futile, as there was a much simpler solution:
1 | :; echo "I'm on unix!!!" # |
Or even better:
1 | ::; echo "Unix!" ; exit |
We then save this as a batch file, and when we execute it on unix, it says “Unix!” but on windows, simply says “Hi”.