4

I am writing a bash script that should execute some Java application that needs a specific classpath.

Further, this script should be executable on both, Ubuntu & Windows (Cygwin).

The problem: The seperator on Windows is ";" and the seperator on Ubuntu is ":". This results in java -cp A.jar;B.jar Main on Windows (also when using cygwin, because it's using Windows' java) and java -cp A.jar:B.jar Main on Ubuntu.

The question: How to detect in the bash script which underlying operating system is running / which java classpath seperator to use?

2 Answers 2

2

A naive (but quick to implement) approach: run uname -a first and push that into a variable. Then check if that output contains "Ubuntu".

If so, you should be using the ":" as separator; otherwise ";" should do.

And if you want to be prepared for other platforms in the future; instead of doing a simple if/else; you might want to check what uname -a has to say on cygwin; to setup some $SEPARATOR_CHAR variable using a bash switch statement.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the hint with uname. Since it should also be able to run on Mac (and probably later other *nix), I think it would be best to check what cygwin says. Then I could use ":" as default, while just switching to ";" when uname matches cygwin's (Windows) name
I'll just let this question open for some more time, probably more answers will come. If not, this will become the accepted answer.
0

Just to provide the final solution:

I just tested uname -s on Ubuntu, Mac, and Windows running cygwin and came up with following script:

if [ "$(uname)" == "Darwin" ]; then
    # Do something under Mac OS X platform
    SEP=":"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
    # Do something under GNU/Linux platform
    SEP=":"
elif [ "$(expr substr $(uname -s) 1 6)" == "CYGWIN" ]; then
    # Do something under Windows NT platform
    SEP=";"
fi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.