-
Notifications
You must be signed in to change notification settings - Fork 11
/
write-envdir
executable file
·37 lines (34 loc) · 1.29 KB
/
write-envdir
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
#!/bin/bash
# usage: write-envdir <envdir> [[var1 [var2 [var3 […]]]]
#
# Writes the current value for each environment variable name given into the
# directory <envdir>, one file per variable. Creates <envdir> if it doesn't
# already exist.
#
# This was copied from the ncov-ingest repo:
# <https://github.com/nextstrain/ncov-ingest/blob/20b432624ad2c4bf24c85756bd04bae1e067bde8/bin/write-envdir>
#
set -eou pipefail
dir="${1:?no envdir path}"
shift
mkdir -pv "$dir"
cd "$dir"
for name in "$@"; do
# We could use [[ -v "$name" ]] if we didn't care about ancient Bash on
# macOS. Since we kinda do—it's useful in dev for folks to be able to run
# programs locally—use `declare | grep` instead. This is imperfect: if
# $name contains regex metachars they'll be interpreted when they shouldn't
# be. Ah well. I don't expect that to actually happen.
#
# We don't use ${!name:+…} or ${!name:-} to detect set/unset because they
# both treat declared-but-empty-string-valued variables as unset. It's a
# legitimate use case to want to set a env var to an empty string.
#
# -trs, 22 May 2024
if ! declare | grep -qE "^$name="; then
echo "error: $name is not set" >&2
exit 1
fi
echo "${!name}" > "$name"
echo "Wrote $dir/$name"
done