Functions
WebDev/*nix assorted functions & hacks
Search
find all
Array
-
Bash
-
Classes
-
Config
-
DateTime
-
Debug
-
dotjs
-
File
-
foooo
-
Git
-
HTML
-
Images
-
Javascript
-
Linux
-
Math
-
Misc
-
MySQL
-
OS X
-
Reading
-
Redis
-
Shell scripts
-
snippets
-
SQL
-
Strings
-
TextMate
-
Web
-
\(^o^)/ Fun!
-
_Misc hacks
-
_Pages
Bash
goto
mcal
2
found
auth
Name:
Category: Bash
Body:
#!/usr/bin/env bash # the definition needs to be at the top (obviously). The sed magic reads in the script file # and looks for "#<label>:", from which point on it prints every line and hands it to eval. # {:a;n;p;ba} = define label a, buffer next line, print it, go to label a (not to be confused with the goto labels) # -n = disable print by default goto () { label=$1; # a label is '#foo:' on a line on its own, with optional leading/trailing whitespace cmd=$(sed -n "/^[[:space:]]*#$label:[[:space:]]*$/{:a;n;p;ba};" $0); if [[ -n $cmd ]]; then # a goto implementation using eval, loving it :-> eval "$cmd"; else echo "E: unknown label"; exit 1; fi; exit; } # gratefully adapted from: https://bobcopeland.com/blog/2012/10/goto-in-bash/ # copy count variable because inside goto, $# is always > 0 args=$# start=${1:-one} goto $start #one: echo process one # this part can be adapted as circumstances require, obviously if [[ $args > 0 ]]; then goto end; fi #two: echo process two if [[ $args > 0 ]]; then goto end; fi #three: echo process three if [[ $args > 0 ]]; then goto end; fi #end: echo END
Footer