-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-platform.sh
executable file
·65 lines (54 loc) · 1.92 KB
/
get-platform.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#/bin/bash
# COMMAND LINE USAGE:
# ./get-platform.sh [--silent]
# WARNING: Because this script is sourced, it could mess up other scripts if we named these just "argCount" and "args"!
getPlaformArgCount=$#
getPlatformArgs=("$@")
for ((i=0; i < $getPlaformArgCount; i++)); do
if [ "${getPlatformArgs[$i]}" = "--silent" ]; then
silentMode=true
fi
done
# These are regexes to compare against $(uname) and $OSTYPE.
# WARNING: The case-insensitive modifier (?i)...(?-i) did NOT work for me
# on MINGW (Windows), so I'm avoiding it in the regex patterns! (using lowercase instead)
windowsNamePattern="(mingw)|(msys)"
macOSNamePattern="darwin"
linuxNamePattern="linux"
lowercaseUName=$(uname | tr '[:upper:]' '[:lower:]')
lowercaseOSType=$(echo "$OSTYPE" | tr '[:upper:]' '[:lower:]')
# Examples values per-platform:
# Windows:
# uname MINGW64_NT-10.0-19044
# $OSTYPE msys
# MacOS:
# uname Darwin
# $OSTYPE darwin21
# Linux:
# uname Linux
# $OSTYPE linux-gnu
simpleOSName="Unknown"
if [[ $lowercaseUName =~ $windowsNamePattern && $lowercaseOSType =~ $windowsNamePattern ]]; then
simpleOSName="Windows"
fi
if [[ $lowercaseUName =~ $macOSNamePattern && $lowercaseOSType =~ $macOSNamePattern ]]; then
simpleOSName="MacOS"
fi
if [[ $lowercaseUName =~ $linuxNamePattern && $lowercaseOSType =~ $linuxNamePattern ]]; then
simpleOSName="Linux"
# NOTE $0 only works when running this script directly.
# When this script is SOURCED from another bash script,
# We must use the BASH_SOURCE variable, which works in both cases.
"$(dirname ${BASH_SOURCE[0]})/pi-check.sh"
exitCode=$?
if [ $exitCode -ne 0 ]; then
isRaspberryPi=true
fi
fi
if [ "$silentMode" != true ]; then
if [[ $isRaspberryPi == true ]]; then
printf "Platform: $simpleOSName (Raspberry Pi)\n"
else
printf "Platform: $simpleOSName\n"
fi
fi