-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperatingSystemFunctions.sh
49 lines (40 loc) · 1.88 KB
/
operatingSystemFunctions.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
#!/usr/bin/env bash
# Provides operating system dependent functions e.g. to detect Windows.
# Requires executeQuery.sh
# Fail on any error ("-e" = exit on first error, "-o pipefail" exist on errors within piped commands)
set -o errexit -o pipefail
## Get this "scripts" directory if not already set
# Even if $BASH_SOURCE is made for Bourne-like shells it is also supported by others and therefore here the preferred solution.
# CDPATH reduces the scope of the cd command to potentially prevent unintended directory changes.
# This way non-standard tools like readlink aren't needed.
SCRIPTS_DIR=${SCRIPTS_DIR:-$( CDPATH=. cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P )} # Repository directory containing the shell scripts
# Return true if the script is running on Windows, otherwise false.
# Example: if isWindows; then echo "Running on Windows"
isWindows() {
[[ "$OSTYPE" == "cygwin" ]] || [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "win32" ]]
}
# Echoes/Returns the first argument if the script is running on Windows, otherwise the second argument.
# Example: artifactPostfix=$(ifWindows "windows.zip" "unix.tar.gz")
ifWindows() {
if isWindows; then
echo "$1"
else
echo "$2"
fi
}
# Prints out a message if Windows was detected for the current OSTYPE or not.
# Example: printWindows
printWindows() {
ifWindows "operatingSystemFunctions: Detected Windows for OSTYPE ${OSTYPE}" "operatingSystemFunctions: No Windows detected for OSTYPE ${OSTYPE}"
}
# Converts the POSIX path given as the first argument to Windows path format if the script is running on Windows.
# Otherwise it just returns the path unchanged (for non-windows systems).
# Example: path=$(convertPosixToWindowsPath "${path}")
convertPosixToWindowsPathIfNecessary() {
if isWindows; then
echo "$1" | sed -e 's/^\///' -e 's/\//\\/g' -e 's/^./\0:/'
else
echo "$1"
fi
}
printWindows