How do I make a script cross compatible?
I have a script that I'm working on to simplify my job. I'd like to share this script with coworkers if it would help them. I use linux and everyone else uses windows. How do I go about making this line open a browser in windows if thats what theyre using?
one(){
xdg-open https://www.liberatedmanuals.com/all.mpl
}
I know I can use "start https://website" to open a webpage in a default browserfor windows but I can't figure out the correct syntax or command to use to get it to recognize which command to use for the appropriate operating system.
Thanks in advance!
PS: Using python as a way to do it may not be an option as I will be unable to add python to the windows machines, unless theyre personal computers. Not even sure if this is possible
Posted Jun 22, 2020, 11:41 PM UTC · edited
Detect the platform and offer two code blocks, one for Windows, one for Linux. Like this:
This is just an example picked at random, there are any number of ways to detect the platform.
Hey that's awesome. Im relatively new to bash and this gives me a lot to work with. Now I know what I have to research to understand how this works. Thanks a lot man
I have to tell you that what I posted will only work if Bash is installed on the Windows platform and the '.sh' suffix is associated with it.
There are ways to write scripts that run cross-platform between Linux and the default Windows shell processor, which is not Bash.
would you be kind to elaborate?
The POSIX standard is roughly the set of common standards which are supported by practically all operating systems, linux, mac, windows, etc.
You can write POSIX compliant shell scripts, which can run natively on any shell, by following the POSIX standard.
One of the most common "almost" POSIX shells is
dash. If you can get your script to run ondash, then it will be able to be used by most people on most operating systems. Look in to the toolshellcheckto check if your script is POSIX compliant or not.This is the worst news I've gotten in 2020 so far. How similar is powershell to bash?
Again, thanks for your advice
If it is, then you live in paradise -- can I visit? :)
Not at all alike. I've been in this situation before with cross-platform installers for my own applications, and after struggling, I create two installers, one for Windows, one for Linux. That's how dissimilar they are.