Skip to content

Commit

Permalink
write-envdir: Explicitly error with a better message when a requested…
Browse files Browse the repository at this point in the history
… var is unset

Eventually I'll dust off my WIP for `nextstrain write-envdir` which does
this sort of nicety and more, and this Bash program will become
obsolete.
  • Loading branch information
tsibley committed May 22, 2024
1 parent d1f65a9 commit b902e77
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions bin/write-envdir
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ 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

0 comments on commit b902e77

Please sign in to comment.