From b902e778d44a8f8c31c0b391dee47bd49a9e7a13 Mon Sep 17 00:00:00 2001 From: Thomas Sibley Date: Wed, 22 May 2024 14:28:08 -0700 Subject: [PATCH] write-envdir: Explicitly error with a better message when a requested 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. --- bin/write-envdir | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/bin/write-envdir b/bin/write-envdir index a190e91..8bd6a1d 100755 --- a/bin/write-envdir +++ b/bin/write-envdir @@ -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