just-bashit 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- just_bashit/__init__.py +3 -0
- just_bashit/_launcher.py +23 -0
- just_bashit/datetime.sh +115 -0
- just_bashit/environment.sh +204 -0
- just_bashit/file.sh +257 -0
- just_bashit/format.sh +223 -0
- just_bashit/function-template.sh +139 -0
- just_bashit/get-jb.sh +177 -0
- just_bashit/inspect.sh +220 -0
- just_bashit/install-deps.sh +300 -0
- just_bashit/just-runit +682 -0
- just_bashit/logging.sh +174 -0
- just_bashit/match.sh +64 -0
- just_bashit/network.sh +172 -0
- just_bashit/path.sh +79 -0
- just_bashit/pkg.sh +155 -0
- just_bashit/script-template +113 -0
- just_bashit/template.toml +80 -0
- just_bashit/toml.sh +331 -0
- just_bashit-0.2.0.dist-info/METADATA +64 -0
- just_bashit-0.2.0.dist-info/RECORD +23 -0
- just_bashit-0.2.0.dist-info/WHEEL +4 -0
- just_bashit-0.2.0.dist-info/entry_points.txt +4 -0
just_bashit/__init__.py
ADDED
just_bashit/_launcher.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""Thin shims that exec bundled bash scripts as console_scripts entry points."""
|
|
2
|
+
import os
|
|
3
|
+
import sys
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
_BIN = Path(__file__).parent
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _exec(script: str) -> None:
|
|
10
|
+
path = _BIN / script
|
|
11
|
+
os.execvp("bash", ["bash", str(path)] + sys.argv[1:])
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def jb() -> None:
|
|
15
|
+
_exec("just-runit")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def jbx() -> None:
|
|
19
|
+
_exec("just-runit")
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def jb_inspect() -> None:
|
|
23
|
+
_exec("inspect.sh")
|
just_bashit/datetime.sh
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# ############################################################################
|
|
3
|
+
# LIBRARY: datetime.sh #
|
|
4
|
+
# PACKAGE: just-bashit version 0.2.0 #
|
|
5
|
+
# ############################################################################
|
|
6
|
+
|
|
7
|
+
# Enforce sourcing of the script by taking advantage of the fact that return
|
|
8
|
+
# only works if sourced and errors otherwise.
|
|
9
|
+
(return 0 2>/dev/null) || (echo "This file must be sourced." && exit)
|
|
10
|
+
|
|
11
|
+
iso-8601-basic() {
|
|
12
|
+
|
|
13
|
+
# Initialize all referenced variables
|
|
14
|
+
local HELP
|
|
15
|
+
local DATE='now'
|
|
16
|
+
local OPTARG=""
|
|
17
|
+
local OPTIND=0
|
|
18
|
+
local DIGITS=0
|
|
19
|
+
local FRACTION=""
|
|
20
|
+
local TIMESTAMP=""
|
|
21
|
+
local MSEC_DIGITS=3
|
|
22
|
+
local USEC_DIGITS=6
|
|
23
|
+
local NANO_DIGITS=9
|
|
24
|
+
|
|
25
|
+
# Create the help
|
|
26
|
+
read -r -d '' HELP <<-'EOF' || true
|
|
27
|
+
Usage: iso-8601-basic [-d DATE] [-m|u|n]
|
|
28
|
+
|
|
29
|
+
Basic-Format ISO 8601 Timestamp.
|
|
30
|
+
|
|
31
|
+
Options:
|
|
32
|
+
-h Show this message and exit.
|
|
33
|
+
-d DATE UTC date and time to use instead of the default 'now'.
|
|
34
|
+
-m Show milliseconds (default is seconds).
|
|
35
|
+
-u Show microseconds (default is seconds).
|
|
36
|
+
-n Show nanoseconds (default is seconds).
|
|
37
|
+
|
|
38
|
+
Path and file-name-friendly characters only are generated:
|
|
39
|
+
|
|
40
|
+
YYYYMMDDThhmmss[.fff[fff]]Z
|
|
41
|
+
|
|
42
|
+
Where
|
|
43
|
+
|
|
44
|
+
- YYYY is the 4 digit year.
|
|
45
|
+
- MM is the two digit month.
|
|
46
|
+
- DD is the two digit day.
|
|
47
|
+
- hh is the two digit hour.
|
|
48
|
+
- mm is the two digit minute.
|
|
49
|
+
- ss is the two digit second.
|
|
50
|
+
- .fff is the 3 digit millisecond.
|
|
51
|
+
- .ffffff is the 6 digit microsecond.
|
|
52
|
+
- .fffffffff is the 9 digit nanosecond.
|
|
53
|
+
EOF
|
|
54
|
+
|
|
55
|
+
# Specify options. Add ':' to start and after options that take arguments.
|
|
56
|
+
while getopts ":hd:mun" option; do
|
|
57
|
+
|
|
58
|
+
# Parse options.
|
|
59
|
+
case $option in
|
|
60
|
+
|
|
61
|
+
# h for help.
|
|
62
|
+
h)
|
|
63
|
+
echo "${HELP}"
|
|
64
|
+
return 0
|
|
65
|
+
;;
|
|
66
|
+
|
|
67
|
+
# Unrecognized options set $option to '?'.
|
|
68
|
+
\?)
|
|
69
|
+
echo "Invalid option: -${OPTARG}"
|
|
70
|
+
echo "${HELP}"
|
|
71
|
+
return 0
|
|
72
|
+
;;
|
|
73
|
+
|
|
74
|
+
# Option taking a parameter stored for later processing.
|
|
75
|
+
d)
|
|
76
|
+
DATE="${OPTARG}"
|
|
77
|
+
;;
|
|
78
|
+
|
|
79
|
+
# Option processed immediately
|
|
80
|
+
m)
|
|
81
|
+
DIGITS="${MSEC_DIGITS}"
|
|
82
|
+
;;
|
|
83
|
+
|
|
84
|
+
# Option processed immediately
|
|
85
|
+
u)
|
|
86
|
+
DIGITS="${USEC_DIGITS}"
|
|
87
|
+
;;
|
|
88
|
+
|
|
89
|
+
# Option processed immediately
|
|
90
|
+
n)
|
|
91
|
+
DIGITS="${NANO_DIGITS}"
|
|
92
|
+
;;
|
|
93
|
+
|
|
94
|
+
esac
|
|
95
|
+
|
|
96
|
+
done
|
|
97
|
+
|
|
98
|
+
# Remove options from the input list $@ so the first remaining argument is $1.
|
|
99
|
+
shift "$((OPTIND - 1))"
|
|
100
|
+
|
|
101
|
+
# Compute the full precision timestamp
|
|
102
|
+
# Use gdate (GNU date) on macOS where system date is BSD date.
|
|
103
|
+
local _date_cmd
|
|
104
|
+
_date_cmd=$(command -v gdate 2>/dev/null || command -v date)
|
|
105
|
+
TIMESTAMP=$("${_date_cmd}" --utc --date="${DATE}" +"%Y%m%dT%H%M%S.%N")
|
|
106
|
+
|
|
107
|
+
# Separate and truncate the seconds if needed
|
|
108
|
+
FRACTION="${TIMESTAMP##*.}"
|
|
109
|
+
if ((DIGITS)); then
|
|
110
|
+
echo "${TIMESTAMP%%.*}.${FRACTION:0:DIGITS}Z"
|
|
111
|
+
else
|
|
112
|
+
echo "${TIMESTAMP%%.*}Z"
|
|
113
|
+
fi
|
|
114
|
+
|
|
115
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# ############################################################################
|
|
3
|
+
# LIBRARY: environment.sh #
|
|
4
|
+
# PACKAGE: just-bashit version 0.2.0 #
|
|
5
|
+
# ############################################################################
|
|
6
|
+
|
|
7
|
+
# Enforce sourcing of the script by taking advantage of the fact that return
|
|
8
|
+
# only works if sourced and errors otherwise.
|
|
9
|
+
(return 0 2>/dev/null) || (echo "This file must be sourced." && exit)
|
|
10
|
+
|
|
11
|
+
set-bashrc() {
|
|
12
|
+
|
|
13
|
+
# Initialize all referenced variables
|
|
14
|
+
local HELP
|
|
15
|
+
local OPTARG=""
|
|
16
|
+
local OPTIND=0
|
|
17
|
+
local BASHRC="${HOME}/.bashrc"
|
|
18
|
+
|
|
19
|
+
# Create the help
|
|
20
|
+
read -r -d '' HELP <<-'EOF' || true
|
|
21
|
+
Usage: set-bashrc [OPTIONS] KEY_OR_ENTRY [VALUE] ...
|
|
22
|
+
|
|
23
|
+
Write a line to ~/.bashrc ONLY if not already present.
|
|
24
|
+
|
|
25
|
+
Options:
|
|
26
|
+
-h Show this message and exit.
|
|
27
|
+
|
|
28
|
+
Arguments:
|
|
29
|
+
KEY_OR_ENTRY Line to write verbatim if given alone, otherwise interpreted
|
|
30
|
+
as a KEY given VALUE is provided to complete the pair. In the
|
|
31
|
+
latter case the line written is "export KEY=VALUE".
|
|
32
|
+
VALUE The value associated with the provided KEY.
|
|
33
|
+
|
|
34
|
+
Note that this function WILL NOT write the same line repeatedly. If the line
|
|
35
|
+
already exists, no write is performed.
|
|
36
|
+
EOF
|
|
37
|
+
|
|
38
|
+
# Specify options. Add ':' to start and after options that take arguments.
|
|
39
|
+
while getopts ":h" option; do
|
|
40
|
+
|
|
41
|
+
# Parse options.
|
|
42
|
+
case $option in
|
|
43
|
+
|
|
44
|
+
# h for help.
|
|
45
|
+
h)
|
|
46
|
+
echo "${HELP}"
|
|
47
|
+
return 0
|
|
48
|
+
;;
|
|
49
|
+
|
|
50
|
+
# Unrecognized options set $option to '?'.
|
|
51
|
+
\?)
|
|
52
|
+
echo "Invalid option: -${OPTARG}"
|
|
53
|
+
echo "${HELP}"
|
|
54
|
+
return 0
|
|
55
|
+
;;
|
|
56
|
+
|
|
57
|
+
esac
|
|
58
|
+
|
|
59
|
+
done
|
|
60
|
+
|
|
61
|
+
# Remove options from the input list $@ so the first remaining argument is $1.
|
|
62
|
+
shift "$((OPTIND - 1))"
|
|
63
|
+
local BASHRC_KEY_OR_ENTRY=${1:-}
|
|
64
|
+
local BASHRC_VALUE=${2:-}
|
|
65
|
+
|
|
66
|
+
# For two inputs assume key-value pair.
|
|
67
|
+
if [ -n "${BASHRC_VALUE}" ]; then
|
|
68
|
+
local BASHRC_ENTRY="export ${BASHRC_KEY_OR_ENTRY}=${BASHRC_VALUE}"
|
|
69
|
+
|
|
70
|
+
# If only one input, use as is.
|
|
71
|
+
elif [ -n "${BASHRC_KEY_OR_ENTRY}" ]; then
|
|
72
|
+
local BASHRC_ENTRY="${BASHRC_KEY_OR_ENTRY}"
|
|
73
|
+
|
|
74
|
+
# Print some help if they didn't provide anything.
|
|
75
|
+
else
|
|
76
|
+
echo "Not enough arguments."
|
|
77
|
+
echo "${HELP}"
|
|
78
|
+
|
|
79
|
+
fi
|
|
80
|
+
|
|
81
|
+
# Check permissions and create if necessary.
|
|
82
|
+
touch "${BASHRC}"
|
|
83
|
+
|
|
84
|
+
# Ensure the file ends with a trailing newline as it should. Get the last
|
|
85
|
+
# character in the file with 'tail', pipe it to 'read' which produces an
|
|
86
|
+
# error if a file doesn't end with a newline, and if so, trigger an echo
|
|
87
|
+
# which produces a newline appending it to the file.
|
|
88
|
+
tail -c1 "${BASHRC}" | read -r _ || echo "" >>"${BASHRC}"
|
|
89
|
+
|
|
90
|
+
# Add entry if not already present using grep options
|
|
91
|
+
# ---------------------------------------------------
|
|
92
|
+
# -q: Quiet; do not write anything to standard output. Exit immediately
|
|
93
|
+
# with 0 status if any match is found, even if an error was detected.
|
|
94
|
+
# -x: Select only those matches that exactly match the whole line.
|
|
95
|
+
# -F: Interpret patterns as fixed strings, not regular expressions.
|
|
96
|
+
grep -qxF "${BASHRC_ENTRY}" "${BASHRC}" || echo "${BASHRC_ENTRY}" >>"${BASHRC}"
|
|
97
|
+
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
unset-bashrc() {
|
|
101
|
+
|
|
102
|
+
# Initialize all referenced variables
|
|
103
|
+
local HELP
|
|
104
|
+
local OPTARG=""
|
|
105
|
+
local OPTIND=0
|
|
106
|
+
local BASHRC="${HOME}/.bashrc"
|
|
107
|
+
|
|
108
|
+
read -r -d '' HELP <<-'EOF' || true
|
|
109
|
+
Usage: unset-bashrc [OPTIONS] KEY_OR_ENTRY [VALUE] ...
|
|
110
|
+
|
|
111
|
+
Remove requested line from ~/.bashrc if present.
|
|
112
|
+
|
|
113
|
+
Options:
|
|
114
|
+
-h Show this message and exit.
|
|
115
|
+
|
|
116
|
+
Arguments:
|
|
117
|
+
KEY_OR_ENTRY Line to remove verbatim if given alone, otherwise interpreted
|
|
118
|
+
as a KEY, given VALUE is provided to complete the pair. In the
|
|
119
|
+
latter case the line searched for is "export KEY=VALUE".
|
|
120
|
+
VALUE The value associated with the provided KEY.
|
|
121
|
+
|
|
122
|
+
Note that this function is a no-op if the given line is not found.
|
|
123
|
+
EOF
|
|
124
|
+
|
|
125
|
+
# Specify options. Add ':' to start and after options that take arguments.
|
|
126
|
+
while getopts ":h" option; do
|
|
127
|
+
|
|
128
|
+
# Parse options.
|
|
129
|
+
case $option in
|
|
130
|
+
|
|
131
|
+
# h for help.
|
|
132
|
+
h)
|
|
133
|
+
echo "${HELP}"
|
|
134
|
+
return 0
|
|
135
|
+
;;
|
|
136
|
+
|
|
137
|
+
# Unrecognized options set $option to '?'.
|
|
138
|
+
\?)
|
|
139
|
+
echo "Invalid option: -${OPTARG}"
|
|
140
|
+
echo "${HELP}"
|
|
141
|
+
return 0
|
|
142
|
+
;;
|
|
143
|
+
|
|
144
|
+
esac
|
|
145
|
+
|
|
146
|
+
done
|
|
147
|
+
|
|
148
|
+
# Remove options from the input list $@ so the first remaining argument is $1.
|
|
149
|
+
shift "$((OPTIND - 1))"
|
|
150
|
+
|
|
151
|
+
# For two inputs assume key-value pair.
|
|
152
|
+
local BASHRC_VALUE=${2:-}
|
|
153
|
+
local BASHRC_KEY_OR_ENTRY=${1:-}
|
|
154
|
+
if [ -n "${BASHRC_VALUE}" ]; then
|
|
155
|
+
local BASHRC_ENTRY="export ${BASHRC_KEY_OR_ENTRY}=${BASHRC_VALUE}"
|
|
156
|
+
|
|
157
|
+
# If only one input, use as is.
|
|
158
|
+
elif [ -n "${BASHRC_KEY_OR_ENTRY}" ]; then
|
|
159
|
+
local BASHRC_ENTRY="${BASHRC_KEY_OR_ENTRY}"
|
|
160
|
+
|
|
161
|
+
# Print some help if they didn't provide anything.
|
|
162
|
+
else
|
|
163
|
+
echo "Not enough arguments."
|
|
164
|
+
echo "${HELP}"
|
|
165
|
+
|
|
166
|
+
fi
|
|
167
|
+
|
|
168
|
+
# Check permissions and create if necessary.
|
|
169
|
+
touch "${BASHRC}"
|
|
170
|
+
|
|
171
|
+
# Remove entry if present using built in editor
|
|
172
|
+
# ---------------------------------------------
|
|
173
|
+
# Delete the exact line in-place; -i'' works on both GNU and BSD sed.
|
|
174
|
+
# shellcheck disable=SC2016
|
|
175
|
+
sed -i'' "/^$(printf '%s' "${BASHRC_ENTRY}" | sed 's/[[\.*^$()+?{}|]/\\&/g')$/d" "${BASHRC}"
|
|
176
|
+
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
check-command-exists() {
|
|
180
|
+
|
|
181
|
+
# Assume we are running under -u so initialize any referenced variables
|
|
182
|
+
local ARG1=${1:-}
|
|
183
|
+
|
|
184
|
+
# Forget getopts, manually parse for handful of inputs
|
|
185
|
+
case "${ARG1}" in
|
|
186
|
+
-h)
|
|
187
|
+
|
|
188
|
+
# Prefer help to commenst since it's user-oriented and runtime-available.
|
|
189
|
+
echo 'Usage: check-command-exists [-h]'
|
|
190
|
+
echo
|
|
191
|
+
echo ' Does just that. Exits with 0 if true 1 if false.'
|
|
192
|
+
echo
|
|
193
|
+
echo 'Options:'
|
|
194
|
+
echo ' -h Show this message and exit.'
|
|
195
|
+
return 0
|
|
196
|
+
;;
|
|
197
|
+
|
|
198
|
+
*)
|
|
199
|
+
command -v "${ARG1}" >/dev/null 2>&1
|
|
200
|
+
;;
|
|
201
|
+
|
|
202
|
+
esac
|
|
203
|
+
|
|
204
|
+
}
|
just_bashit/file.sh
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# ############################################################################
|
|
3
|
+
# LIBRARY: file.sh #
|
|
4
|
+
# PACKAGE: just-bashit version 0.2.0 #
|
|
5
|
+
# ############################################################################
|
|
6
|
+
|
|
7
|
+
# Enforce sourcing of the script by taking advantage of the fact that return
|
|
8
|
+
# only works if sourced and errors otherwise.
|
|
9
|
+
(return 0 2>/dev/null) || (echo "This file must be sourced." && exit)
|
|
10
|
+
|
|
11
|
+
add-line() {
|
|
12
|
+
|
|
13
|
+
# Initialize all referenced variables
|
|
14
|
+
local HELP
|
|
15
|
+
local BLANK=1
|
|
16
|
+
local OPTARG=""
|
|
17
|
+
local OPTIND=0
|
|
18
|
+
|
|
19
|
+
# Create the help
|
|
20
|
+
read -r -d '' HELP <<-'EOF' || true
|
|
21
|
+
Usage: add-line [OPTIONS] [ENTRY] FILEPATH ...
|
|
22
|
+
|
|
23
|
+
Write ENTRY to FILEPATH only if not already present or if
|
|
24
|
+
one argument is given, in which case the argument is taken as
|
|
25
|
+
FILEPATH and a blank line is written.
|
|
26
|
+
|
|
27
|
+
Options:
|
|
28
|
+
-h Show this message and exit.
|
|
29
|
+
-x Don't write blank lines.
|
|
30
|
+
|
|
31
|
+
Arguments:
|
|
32
|
+
ENTRY Line to write verbatim.
|
|
33
|
+
FILEPATH Path to file for writing.
|
|
34
|
+
|
|
35
|
+
Note that this function WILL NOT write the same line repeatedly. Except
|
|
36
|
+
for the blankline case, if the line already exists, no write is performed.
|
|
37
|
+
EOF
|
|
38
|
+
|
|
39
|
+
# Specify options. Add ':' to start and after options that take arguments.
|
|
40
|
+
while getopts ":hx" option; do
|
|
41
|
+
|
|
42
|
+
# Parse options.
|
|
43
|
+
case $option in
|
|
44
|
+
|
|
45
|
+
# h for help.
|
|
46
|
+
h)
|
|
47
|
+
echo "${HELP}"
|
|
48
|
+
return 0
|
|
49
|
+
;;
|
|
50
|
+
|
|
51
|
+
# Dont write blank lines.
|
|
52
|
+
x)
|
|
53
|
+
local BLANK=0
|
|
54
|
+
;;
|
|
55
|
+
|
|
56
|
+
# Unrecognized options set $option to '?'.
|
|
57
|
+
\?)
|
|
58
|
+
echo "Invalid option: -${OPTARG}"
|
|
59
|
+
echo "${HELP}"
|
|
60
|
+
return 0
|
|
61
|
+
;;
|
|
62
|
+
|
|
63
|
+
esac
|
|
64
|
+
|
|
65
|
+
done
|
|
66
|
+
|
|
67
|
+
# Remove options from the input list $@ so the first remaining argument is $1.
|
|
68
|
+
shift "$((OPTIND - 1))"
|
|
69
|
+
local ENTRY=${1:-}
|
|
70
|
+
local FILEPATH=${2:-}
|
|
71
|
+
|
|
72
|
+
# For two inputs assume standard operation; if one, treat it as FILEPATH.
|
|
73
|
+
if [ -z "${FILEPATH}" ] && [ -n "${ENTRY}" ]; then
|
|
74
|
+
FILEPATH="${ENTRY}"
|
|
75
|
+
ENTRY=""
|
|
76
|
+
|
|
77
|
+
# Print some help if they didn't provide anything.
|
|
78
|
+
else
|
|
79
|
+
echo "Not enough arguments."
|
|
80
|
+
echo "${HELP}"
|
|
81
|
+
|
|
82
|
+
fi
|
|
83
|
+
|
|
84
|
+
# Check permissions and create if necessary.
|
|
85
|
+
touch "${FILEPATH}"
|
|
86
|
+
|
|
87
|
+
if ((BLANK)) && [[ -z ${ENTRY} ]]; then
|
|
88
|
+
echo "" >>"${FILEPATH}"
|
|
89
|
+
else
|
|
90
|
+
# Add entry if not already present using grep options
|
|
91
|
+
# ---------------------------------------------------
|
|
92
|
+
# -q: Quiet; do not write anything to standard output. Exit immediately
|
|
93
|
+
# with 0 status if any match is found, even if an error was detected.
|
|
94
|
+
# -x: Select only those matches that exactly match the whole line.
|
|
95
|
+
# -F: Interpret patterns as fixed strings, not regular expressions.
|
|
96
|
+
grep -qxF "${ENTRY}" "${FILEPATH}" || echo "${ENTRY}" >>"${FILEPATH}"
|
|
97
|
+
|
|
98
|
+
# Ensure the file ends with a trailing newline as it should. Get the last
|
|
99
|
+
# character in the file with 'tail', pipe it to 'read' which produces an
|
|
100
|
+
# error if a file doesn't end with a newline, and if so, trigger an echo
|
|
101
|
+
# which produces a newline appending it to the file.
|
|
102
|
+
tail -c1 "${FILEPATH}" | read -r _ || echo "" >>"${FILEPATH}"
|
|
103
|
+
fi
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
remove-line() {
|
|
107
|
+
|
|
108
|
+
# Initialize all referenced variables
|
|
109
|
+
local HELP
|
|
110
|
+
local BLANK=1
|
|
111
|
+
local OPTARG=""
|
|
112
|
+
local OPTIND=0
|
|
113
|
+
|
|
114
|
+
read -r -d '' HELP <<-'EOF' || true
|
|
115
|
+
Usage: remove-line [OPTIONS] [ENTRY] FILEPATH ...
|
|
116
|
+
|
|
117
|
+
Remove ENTRY from FILEPATH if present.
|
|
118
|
+
|
|
119
|
+
Options:
|
|
120
|
+
-h Show this message and exit.
|
|
121
|
+
-x Don't remove blank lines.
|
|
122
|
+
|
|
123
|
+
Arguments:
|
|
124
|
+
ENTRY Line to remove verbatim.
|
|
125
|
+
FILEPATH Path to file for line removal.
|
|
126
|
+
|
|
127
|
+
Note that this function is a no-op if the given line is not found.
|
|
128
|
+
EOF
|
|
129
|
+
|
|
130
|
+
# Specify options. Add ':' to start and after options that take arguments.
|
|
131
|
+
while getopts ":hx" option; do
|
|
132
|
+
|
|
133
|
+
# Parse options.
|
|
134
|
+
case $option in
|
|
135
|
+
|
|
136
|
+
# h for help.
|
|
137
|
+
h)
|
|
138
|
+
echo "${HELP}"
|
|
139
|
+
return 0
|
|
140
|
+
;;
|
|
141
|
+
|
|
142
|
+
# Dont remove blank lines.
|
|
143
|
+
x)
|
|
144
|
+
local BLANK=0
|
|
145
|
+
;;
|
|
146
|
+
|
|
147
|
+
# Unrecognized options set $option to '?'.
|
|
148
|
+
\?)
|
|
149
|
+
echo "Invalid option: -${OPTARG}"
|
|
150
|
+
echo "${HELP}"
|
|
151
|
+
return 0
|
|
152
|
+
;;
|
|
153
|
+
|
|
154
|
+
esac
|
|
155
|
+
|
|
156
|
+
done
|
|
157
|
+
|
|
158
|
+
# Remove options from the input list $@ so the first remaining argument is $1.
|
|
159
|
+
shift "$((OPTIND - 1))"
|
|
160
|
+
local ENTRY=${1:-}
|
|
161
|
+
local FILEPATH=${2:-}
|
|
162
|
+
|
|
163
|
+
# For two inputs assume standard operation; if one, treat it as FILEPATH.
|
|
164
|
+
if [ -z "${FILEPATH}" ]; then
|
|
165
|
+
if [ -n "${ENTRY}" ]; then
|
|
166
|
+
if ((BLANK)); then
|
|
167
|
+
FILEPATH="${ENTRY}"
|
|
168
|
+
ENTRY=""
|
|
169
|
+
else
|
|
170
|
+
return 0
|
|
171
|
+
fi
|
|
172
|
+
else
|
|
173
|
+
echo "Not enough arguments."
|
|
174
|
+
echo "${HELP}"
|
|
175
|
+
fi
|
|
176
|
+
fi
|
|
177
|
+
|
|
178
|
+
# Check permissions and create if necessary.
|
|
179
|
+
touch "${FILEPATH}"
|
|
180
|
+
|
|
181
|
+
# Remove entry if present using built in editor
|
|
182
|
+
# ---------------------------------------------
|
|
183
|
+
# Delete the exact line in-place; -i'' works on both GNU and BSD sed.
|
|
184
|
+
# shellcheck disable=SC2016
|
|
185
|
+
sed -i'' "/^$(printf '%s' "${ENTRY}" | sed 's/[[\.*^$()+?{}|]/\\&/g')$/d" "${FILEPATH}"
|
|
186
|
+
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
add-contents() {
|
|
190
|
+
|
|
191
|
+
# Initialize all referenced variables
|
|
192
|
+
local HELP
|
|
193
|
+
local BLANK=1
|
|
194
|
+
local OPTARG=""
|
|
195
|
+
local OPTIND=0
|
|
196
|
+
|
|
197
|
+
# Create the help
|
|
198
|
+
read -r -d '' HELP <<-'EOF' || true
|
|
199
|
+
Usage: add-contents [OPTIONS] FROMPATH TOPATH ...
|
|
200
|
+
|
|
201
|
+
Write each line of FROMPATH to TOPATH only if not
|
|
202
|
+
already present in TOPATH.
|
|
203
|
+
|
|
204
|
+
Options:
|
|
205
|
+
-h Show this message and exit.
|
|
206
|
+
-x Don't write blank lines.
|
|
207
|
+
|
|
208
|
+
Arguments:
|
|
209
|
+
FROMPATH Path to file for reading lines.
|
|
210
|
+
TOPATH Path to file for writing lines.
|
|
211
|
+
|
|
212
|
+
EOF
|
|
213
|
+
|
|
214
|
+
# Specify options. Add ':' to start and after options that take arguments.
|
|
215
|
+
while getopts ":hx" option; do
|
|
216
|
+
|
|
217
|
+
# Parse options.
|
|
218
|
+
case $option in
|
|
219
|
+
|
|
220
|
+
# h for help.
|
|
221
|
+
h)
|
|
222
|
+
echo "${HELP}"
|
|
223
|
+
return 0
|
|
224
|
+
;;
|
|
225
|
+
|
|
226
|
+
# Dont write blank lines.
|
|
227
|
+
x)
|
|
228
|
+
local BLANK=0
|
|
229
|
+
;;
|
|
230
|
+
|
|
231
|
+
# Unrecognized options set $option to '?'.
|
|
232
|
+
\?)
|
|
233
|
+
echo "Invalid option: -${OPTARG}"
|
|
234
|
+
echo "${HELP}"
|
|
235
|
+
return 0
|
|
236
|
+
;;
|
|
237
|
+
|
|
238
|
+
esac
|
|
239
|
+
|
|
240
|
+
done
|
|
241
|
+
|
|
242
|
+
# Remove options from the input list $@ so the first remaining argument is $1.
|
|
243
|
+
shift "$((OPTIND - 1))"
|
|
244
|
+
local FROMPATH=${1:-}
|
|
245
|
+
local TOPATH=${2:-}
|
|
246
|
+
|
|
247
|
+
# Read FROMPATH file line by line
|
|
248
|
+
while IFS= read -r line; do
|
|
249
|
+
# Write to TOPATH
|
|
250
|
+
if ((BLANK)); then
|
|
251
|
+
add-line "${line}" "${TOPATH}"
|
|
252
|
+
else
|
|
253
|
+
add-line -x "${line}" "${TOPATH}"
|
|
254
|
+
fi
|
|
255
|
+
done <"$FROMPATH"
|
|
256
|
+
|
|
257
|
+
}
|