Created
July 27, 2024 02:43
-
-
Save karanlyons/5c0a7b9497220b6ebfabe3e936746dfb to your computer and use it in GitHub Desktop.
I Can’t Protect You From Writing Bash, But I Can Protect You From Unset Bash Variables (Why Do You Need Protection From Unset Bash Variables?)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -CEepuo pipefail | |
function cleanup() { | |
trap - EXIT; | |
if [ -n "${vars_in}" ]; then rm -f "${vars_in}"; fi; | |
if [ -n "${vars_out}" ]; then rm -f "${vars_out}"; fi; | |
} | |
trap 'cleanup' EXIT; | |
readonly vars_in=$(mktemp -u); mkfifo "${vars_in}"; | |
readonly vars_out=$(mktemp -u); mkfifo "${vars_out}"; | |
( | |
while :; do | |
while read -r var; do | |
echo "${!var}" > "${vars_out}"; | |
done < "${vars_in}"; | |
done; | |
) & | |
function read_var() { | |
readonly var="$1"; | |
echo "${var}" > "${vars_in}"; | |
cat "${vars_out}"; | |
} | |
function main() { | |
echo "[l] SECONDS: ${SECONDS}"; | |
echo "[c] SECONDS: $(read_var SECONDS)"; | |
sleep 1; | |
echo "[l] SECONDS: ${SECONDS}"; | |
echo "[c] SECONDS: $(read_var SECONDS)"; | |
unset SECONDS; SECONDS='unset'; | |
sleep 1; | |
echo "[l] SECONDS: ${SECONDS}"; | |
echo "[c] SECONDS: $(read_var SECONDS)"; | |
sleep 1; | |
echo "[l] SECONDS: ${SECONDS}"; | |
echo "[c] SECONDS: $(read_var SECONDS)"; | |
} | |
main; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment