R: Determine If a Script Is Running in Windows or Linux

R: determine if a script is running in Windows or Linux

if(.Platform$OS.type == "unix") {
} else {

}

How can I determine in R what platform I'm running on?

Alternatives to R.version()$os are .Platform$OS.type and R.Version()$platform. See ?.Platform for further info. Note that Sys.info() is not implemented on all platforms, but does give the key information on which platform R is running whereas the others give info on the platform under which R was built.

How to check if running in Cygwin, Mac or Linux?

Usually, uname with its various options will tell you what environment you're running in:

pax> uname -a
CYGWIN_NT-5.1 IBM-L3F3936 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin

pax> uname -s
CYGWIN_NT-5.1

And, according to the very helpful schot (in the comments), uname -s gives Darwin for OSX and Linux for Linux, while my Cygwin gives CYGWIN_NT-5.1. But you may have to experiment with all sorts of different versions.

So the bash code to do such a check would be along the lines of:

unameOut="$(uname -s)"
case "${unameOut}" in
Linux*) machine=Linux;;
Darwin*) machine=Mac;;
CYGWIN*) machine=Cygwin;;
MINGW*) machine=MinGw;;
*) machine="UNKNOWN:${unameOut}"
esac
echo ${machine}

Note that I'm assuming here that you're actually running within CygWin (the bash shell of it) so paths should already be correctly set up. As one commenter notes, you can run the bash program, passing the script, from cmd itself and this may result in the paths not being set up as needed.

If you are doing that, it's your responsibility to ensure the correct executables (i.e., the CygWin ones) are being called, possibly by modifying the path beforehand or fully specifying the executable locations (e.g., /c/cygwin/bin/uname).

How can I determine if the operating system a Python script is running on is Unix-like?

The Pythonic way to do it is not to care what platform you are on.

If there are multiple different facilities to accomplish something depending on the platform, then abstract them behind a function or class, which should try a facility and move on to another if that facility is not available on the current platform.

How to detect the OS from a Bash script?

I think the following should work. I'm not sure about win32 though.

if [[ "$OSTYPE" == "linux-gnu"* ]]; then
# ...
elif [[ "$OSTYPE" == "darwin"* ]]; then
# Mac OSX
elif [[ "$OSTYPE" == "cygwin" ]]; then
# POSIX compatibility layer and Linux environment emulation for Windows
elif [[ "$OSTYPE" == "msys" ]]; then
# Lightweight shell and GNU utilities compiled for Windows (part of MinGW)
elif [[ "$OSTYPE" == "win32" ]]; then
# I'm not sure this can happen.
elif [[ "$OSTYPE" == "freebsd"* ]]; then
# ...
else
# Unknown.
fi

BASH: Determine if script was called from virtual machine (Ubuntu), or the W10 bash app?

I have used this for a long time successfully:

if [[ "$(uname -r)" == *Microsoft ]]; then
do stuff
fi

Shell script to check if running in Windows when using WSL?

Using uname -r does the trick

According to https://github.com/microsoft/WSL/issues/423#issuecomment-608236476, if you use

uname -r | sed -n 's/.*\( *Microsoft *\).*/\1/ip'

You'll get output as "Microsoft" in case it is WSL. Otherwise, you should get no output.

So you can use something like

if [ $(uname -r | sed -n 's/.*\( *Microsoft *\).*/\1/ip') ];
then
echo "This is Windows WSL baby!"
else
echo "Not Windows"
fi

How to check the OS within R

use Sys.info() for all information about the system, Sys.info()['sysname'] gives you the OS.

R.Version() gives you the version of R, including which architecture you're running (32bit - i386 - versus 64bit - x64 - ).

R.home() and system.file(package="xxx") give you information of the location of the root resp. the package files.



Related Topics



Leave a reply



Submit