r/bash•
syncspark

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

1513
lutusp•

Detect the platform and offer two code blocks, one for Windows, one for Linux. Like this:

if uname &>/dev/null; then
    # Linux code here
else
    # Windows code here
fi

This is just an example picked at random, there are any number of ways to detect the platform.

15
syncsparkOP•

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

2
lutusp•

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.

3
foadsf•

would you be kind to elaborate?

1

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 on dash, then it will be able to be used by most people on most operating systems. Look in to the tool shellcheck to check if your script is POSIX compliant or not.

2
syncsparkOP•

This is the worst news I've gotten in 2020 so far. How similar is powershell to bash?

Again, thanks for your advice

1
lutusp•

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.

This is the worst news I've gotten in 2020 so far.

If it is, then you live in paradise -- can I visit? :)

How similar is powershell to bash?

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.

1
am_katzest/bin/dash | lolcat•

an if statement using uname or some environmental $VARIABLE?

3
syncsparkOP•

I figured it would be an if statement using a variable but I couldn't figure out how to use it. I'm still new to this so I'm slowly picking it up. Thanks for the help

1
JustAnotherITUser•

I use MSYS2 for a bash shell in Windows, so my example may not work depending on how you're executing the script on a Windows platform...but:

if command -v "cmd.exe" >/dev/null; then
    # Windows command
else
    # xdg-open
fi

You could also use type if command is not available.

I'd likely set a variable in the script, something like:

winhost="$(command -v "cmd.exe")"

Then, if sections of the script where I need to run different commands for each host type, you could use

if [ -n "${winhost}" ]; then
    # Windows command
else
    # xdg-open
fi

or

if [ -z "${winhost}" ]; then
    # xdg-open
else
    # Windows command
fi

As I do not have WSL1/2 set up on this machine, I'm unsure if this will work in that environment. You'll need to do some testing.

2
IchBinEineFrage•

You can grep /proc/sys/kernel/osrelease for Microsoft to detect if you're in wsl.

2
syncsparkOP•

Much appreciated man! I have several .sh files that pretty much do the same thing that I'm using for testing out all of your examples so this is all super helpful. Thanks again

1
[deleted]•

[deleted]

1