Skip to content

Instantly share code, notes, and snippets.

@hyperupcall
Last active August 21, 2022 05:01
Show Gist options
  • Save hyperupcall/ce66d6e9b01ddfaad657be0dbb3e495b to your computer and use it in GitHub Desktop.
Save hyperupcall/ce66d6e9b01ddfaad657be0dbb3e495b to your computer and use it in GitHub Desktop.
Dumb Bash Goto
# shellcheck shell=bash
goto() {
local current_file="$0"
local label="$1"
# shellcheck disable=SC1090
source <(
local eval_line='no'
local regex=$'[ \t]*: :'"$label"
while read -r line || [ -n "$line" ]; do
if [ "$eval_line" = 'yes' ]; then
printf '%s\n' "$line"
else
# Preserve line numbers
printf '%s\n' "#$line"
if [[ "$line" =~ $regex ]]; then
eval_line=yes
fi
fi
done < "$current_file"; unset line
)
exit
}
declare -i n=0
: :LABEL1
main() {
n=$((n+1))
printf '%d\n' "$n"
if ((n == 10)); then
printf '%s\n' "Done."
return 0
fi
goto LABEL1
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment