Detect Windows or Linux in C, C++
It's generally done like this (more or less):
#ifdef _WIN32
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#define DIV 1048576
#define WIDTH 7
#endif
#ifdef linux
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#endif
int main(int argc, char *argv[])
{
#ifdef _WIN32
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
_tprintf (TEXT("There is %*ld %% of memory in use.\n"),
WIDTH, statex.dwMemoryLoad);
#endif
#ifdef linux
char cmd[30];
int flag = 0;
FILE *fp;
char line[130];
int TotalMem, TotalFree, TotalUsed;
flag=0;
memcpy (cmd,"\0",30);
sprintf(cmd,"free -t -m|grep Total");
fp = popen(cmd, "r");
while ( fgets( line, sizeof line, fp))
{
flag++;
sscanf(line,"%*s %d %d %d",&TotalMem, &TotalUsed, &TotalFree);
}
pclose(fp);
if(flag)
printf("TotalMem:%d -- TotalUsed:%d -- TotalFree:%d\n",TotalMem,TotalUsed,TotalFree);
else
printf("not found\n");
#endif
return 0;
}
This way, only code for linux will be compiled while on a linux platform, and only windows code will be compiled on a windows platform.
How to detect Linux or macOS or Windows in powershell?
The problem is Powershell 5.1.
Detailed Here: https://devblogs.microsoft.com/scripting/powertip-determine-your-version-of-powershell-and-host-operating-system/
More Detail Here: Determine the OS version, Linux and Windows from Powershell
Powershell 5.1 does not have the capacity nor ability to determine OS outside of the Microsoft environment. The best you can do is, using get-wmi or cim-sesssion
windows
!windows
For PowerShell Core (Powershell Version 6.0+), you can use Automatic Variables:
$IsLinux
$IsMacOS
$IsWindows
With 6+ you can do something to the effect of:
foreach ($i in $info) {
if ($i -eq $IsLinux) {
Write-Host $i is Linux
}
elseif ($i -eq $IsMacOS) {
Write-Host $i is This is a dirty, dirty Mac
}
elseif ($i -eq $IsWindows) {
Write-Host $i is Windows
}
}
To bring this to a close what you are asking for is simply not possible / possibly not worth the effort with PowerShell 5.1.
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
PHP script - detect whether running under linux or Windows?
Check the value of the PHP_OS
constantDocs.
It will give you various values on Windows like WIN32
, WINNT
or Windows
.
See as well: Possible Values For: PHP_OS and php_uname
Docs:
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
echo 'This is a server using Windows!';
} else {
echo 'This is a server not using Windows!';
}
Reliably detect Windows in Python
>>> import platform
>>> platform.system()
'Windows'
How to determine that Java is running on Windows
After noting the various comments and other posts, I've not found a reason not to continue using the isWindows
check I currently have in my code, which is simply the first test listed:
boolean isWindows = System.getProperty("os.name").startsWith("Windows");
The first six tests in my question appear to detect isWindows
correctly, but not the last item File.separatorChar == '\\'
(as that is also true on OS/2).
Note that in Turkish the lower case value of "I" is not equal to "i". Try:
"I".toLowerCase(Locale.forLanguageTag("TR")).equals("i") // char:305
"i".toUpperCase(Locale.forLanguageTag("TR")).equals("I") // char:304
Maybe this explains why some developers use .toLowerCase(Locale.ENGLISH)
but nothing changes for any of the test results unless os.name
property is uppercase in Turkish installation.
Thanks for all the suggestions.
Determine the OS version, Linux and Windows from Powershell
Aren't there environment variables you can view on the other platforms for the OS?
Get-ChildItem -Path Env:
Particularly, on Windows at least, there's an OS environment variable, so you should be able to accomplish this by using $Env:OS
.
Since some time has passed and the PowerShell Core (v6) product is GA now (the Core branding has been dropped as of v7), you can more accurately determine your platform based on the following automatic boolean variables:
$IsMacOS
$IsLinux
$IsWindows
Detect OS with python
Use the platform
module:
import platform
print(platform.system())
print(platform.release())
print(platform.version())
Note that a system running on Mac will return 'Darwin' for platform.system()
platform.platform()
will return extremely detailed data, such as
'Linux-3.3.0-8.fc16.x86_64-x86_64-with-fedora-16-Verne'
How to identify on which OS Python is running on?
>>> import os
>>> os.name
'posix'
>>> import platform
>>> platform.system()
'Linux'
>>> platform.release()
'2.6.22-15-generic'
The output of platform.system()
is as follows:
- Linux:
Linux
- Mac:
Darwin
- Windows:
Windows
See: platform
— Access to underlying platform’s identifying data
Related Topics
Classcastexception When Casting to the Same Class
Non Resizable Window Border and Positioning
Why Must Wait() Always Be in Synchronized Block
How to Throw Checked Exceptions from Inside Java 8 Streams
When to Use Generic Methods and When to Use Wild-Card
Does Spring @Transactional Attribute Work on a Private Method
Maximum Java Heap Size of a 32-Bit Jvm on a 64-Bit Os
Double Decimal Formatting in Java
How to Compare Two Double Values in Java
What's Java Hybrid - Applet + Application
Why Does Reading from Process' Inputstream Block Altough Data Is Available
Javafx: Could Not Find or Load Main Class Only on Linux
How to Open a Unc Path from Linux in Java
Kiosk Mode for Linux Java Swing Application
How to Set Ld_Library_Path for Java Process