Skip to content

Instantly share code, notes, and snippets.

@MacsInSpace
Last active December 5, 2023 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MacsInSpace/38cb4d033d936934e433ab625c589bef to your computer and use it in GitHub Desktop.
Save MacsInSpace/38cb4d033d936934e433ab625c589bef to your computer and use it in GitHub Desktop.
Detect OS and Architecture cross platform testing
#!/usr/bin/env bash
@echo off
goto(){
function checkOS() {
unameOut="$(uname -s)"
case "${unameOut}" in
*Microsoft*) OS="WSL";; #must be first since Windows subsystem for linux will have Linux in the name too
*microsoft*) OS="WSL2";;
Linux*) OS="Linux";;
Darwin*) OS="MacOS";;
CYGWIN*) OS="Cygwin";;
MINGW*) OS="Windows";;
*Msys) OS="Windows";;
*) OS="UNKNOWN:${unameOut}"
esac
echo ${OS}
}
function checkProcessor() {
arch="$(uname -m)"
if [[ "$arch" = x86_64* ]]; then
if [[ "$(uname -a)" = *ARM64* ]]; then
echo 'ARM64'
else
echo 'x86_64'
fi # was going to use /usr/sbin/sysctl -n machdep.cpu.brand_string but this nested if works detecting ARM for now. I think
elif [[ "$arch" = i*86 ]]; then
echo 'i*86'
elif [[ "$arch" = arm* ]]; then
echo 'ARM64'
elif test "$arch" = aarch64; then
echo 'aarch64'
else
echo "UNKNOWN:${arch}"
fi
}
}
checkOS
checkProcessor
goto $@
read -rsp $'Press enter to continue...\n'
exit
:(){
rem Windows script here
FOR /F "tokens=*" %%o IN ('echo %OS%') do (SET unameOut=%%o)
FOR /F "tokens=*" %%p IN ('echo %PROCESSOR_ARCHITECTURE%') do (SET arch=%%p)
echo "Operating System is %unameOut%"
echo "Processor Architecture is %arch%"
rem pause
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment