-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_gnucompat.sh
More file actions
173 lines (151 loc) · 3.25 KB
/
lib_gnucompat.sh
File metadata and controls
173 lines (151 loc) · 3.25 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env bash
###
### GNUTools Compatibility Layer
###
### This script provides compatibility functions for GNU tools on non-GNU
### systems, such as macOS.
###
### Usage:
###
### SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
### readonly SCRIPT_DIR
### source "${SCRIPT_DIR}/lib-bash/lib_gnucompat.sh"
###
# Guard variable to prevent multiple imports
if [[ -n "${LIB_GNUCOMPAT_SH_INCLUDED:-}" ]]; then
return
fi
LIB_GNUCOMPAT_SH_INCLUDED=1
LIB_GNUCOMPAT_PATH="$(realpath "${BASH_SOURCE[0]}")"
readonly LIB_GNUCOMPAT_PATH
LIB_GNUCOMPAT_DIR="$(dirname "${LIB_GNUCOMPAT_PATH}")"
readonly LIB_GNUCOMPAT_DIR
# shellcheck source=/dev/null
source "${LIB_GNUCOMPAT_DIR}/lib_init.sh"
# ------------------------------------------------------------------------------
# Compatibility Functions
#
# These functions will return the corresponding GNU tool on non-GNU systems.
# Such as `gsed` on macOS, which is the GNU version of `sed`.
# ------------------------------------------------------------------------------
# GNU `sed` command
if command -v gsed &>/dev/null; then
readonly SED="gsed"
else
readonly SED="sed"
fi
export SED
# GNU `awk` command
if command -v gawk &>/dev/null; then
readonly AWK="gawk"
else
readonly AWK="awk"
fi
export AWK
# GNU `grep` command
if command -v ggrep &>/dev/null; then
readonly GREP="ggrep"
else
readonly GREP="grep"
fi
export GREP
# GNU `find` command
if command -v gfind &>/dev/null; then
readonly FIND="gfind"
else
readonly FIND="find"
fi
export FIND
# GNU `sort` command
if command -v gsort &>/dev/null; then
readonly SORT="gsort"
else
readonly SORT="sort"
fi
export SORT
# GNU `tar` command
if command -v gtar &>/dev/null; then
readonly TAR="gtar"
else
readonly TAR="tar"
fi
export TAR
# GNU `date` command
if command -v gdate &>/dev/null; then
readonly DATE="gdate"
else
readonly DATE="date"
fi
export DATE
# GNU `xargs` command
if command -v gxargs &>/dev/null; then
readonly XARGS="gxargs"
else
readonly XARGS="xargs"
fi
export XARGS
# GNU `cut` command
if command -v gcut &>/dev/null; then
readonly CUT="gcut"
else
readonly CUT="cut"
fi
export CUT
# GNU `head` command
if command -v ghead &>/dev/null; then
readonly HEAD="ghead"
else
readonly HEAD="head"
fi
export HEAD
# GNU `tail` command
if command -v gtail &>/dev/null; then
readonly TAIL="gtail"
else
readonly TAIL="tail"
fi
export TAIL
# GNU `tr` command
if command -v gtr &>/dev/null; then
readonly TR="gtr"
else
readonly TR="tr"
fi
export TR
# GNU `uniq` command
if command -v guniq &>/dev/null; then
readonly UNIQ="guniq"
else
readonly UNIQ="uniq"
fi
export UNIQ
# GNU `wc` command
if command -v gwc &>/dev/null; then
readonly WC="gwc"
else
readonly WC="wc"
fi
export WC
# GNU `diff` command
if command -v gdiff &>/dev/null; then
readonly DIFF="gdiff"
else
readonly DIFF="diff"
fi
export DIFF
# Signal, if the given tool is a GNU tool
function is_gnu() {
local cmd="${1:?Error: No command provided to is_gnu function}"
# Check if the command is available
if ! command -v "${cmd}" &>/dev/null; then
return 0
fi
("${cmd}" --version &>/dev/null) || return 1
local version_output
version_output="$("${cmd}" --version 2>&1)"
if [[ "${version_output}" == *"GNU"* ]]; then
return 0
else
return 1
fi
}