-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdot_sh_functions
69 lines (59 loc) · 1.91 KB
/
dot_sh_functions
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
66
67
68
69
# SPDX-FileCopyrightText: Chris Wilson <[email protected]>
#
# SPDX-License-Identifier: MIT
# Shell functions for Bourne-compatible shells
# shellcheck shell=sh
# ------------------------------------------------------------------------------
# Functions
# ------------------------------------------------------------------------------
# Checks if a command exists (works for executables, builtins, aliases, and
# functions)
# https://stackoverflow.com/a/34143401
command_exists ()
{
command -v "$1" >/dev/null 2>&1
}
# Checks if an executable exists (doesn't work for )
# https://stackoverflow.com/a/26759734
executable_exists ()
(
cmd=$(command -v "$1")
[ -x "${cmd}" ]
)
# https://stackoverflow.com/a/18434831
os_type ()
{
uname | tr '[:upper:]' '[:lower:]'
}
# Macs with Apple Silicon (arm64) can run executables compiled for Intel
# (x86_64) via the Rosetta translation process. See this link for more details:
# https://developer.apple.com/documentation/apple-silicon/about-the-rosetta-translation-environment
case $(os_type) in
darwin*)
# https://stackoverflow.com/questions/65259300/detect-apple-silicon-from-command-line#comment124415881_65259353
# https://stackoverflow.com/a/37073155
cpu_is_apple_silicon ()
(
cpu=$(sysctl -n machdep.cpu.brand_string)
[ "${cpu#Apple}" != "${cpu}" ]
)
cpu_is_intel ()
(
cpu=$(sysctl -n machdep.cpu.brand_string)
[ "${cpu#Intel}" != "${cpu}" ]
)
# https://indiespark.top/software/detecting-apple-silicon-shell-script/
shell_is_translated ()
(
translated=$(sysctl -n sysctl.proc_translated)
[ "${translated}" = "1" ]
)
shell_is_native ()
(
translated=$(sysctl -n sysctl.proc_translated)
[ "${translated}" = "0" ]
)
;;
*)
;;
esac