Created
April 20, 2015 22:16
-
-
Save Syrup-tan/77975470378ea50fc67d to your computer and use it in GitHub Desktop.
A simple standard library for posix sh, WIP
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/sh | |
## sh stdlib | |
plus_equals() { FUNCTION="plus_equals()"; | |
## Handle options | |
case "$1" in | |
-h) | |
echo; | |
echo "$FUNCTION: add an amount to the variable"; | |
echo; | |
echo ' usage: plus_equals -h'; | |
echo ' usage: plus_equals [--] variable amount'; | |
echo; | |
return 1; | |
;; | |
--) shift; ;; | |
-*) throw "$FUNCTION: Invalid option: $1"; return 1; ;; | |
esac; | |
## Get the arguments | |
VARIABLE="$1"; | |
AMOUNT="$2"; | |
## Check if the variable is empty | |
if [ -z "$VARIABLE" ]; then | |
throw "$FUNCTION: Empty variable name."; | |
return 1; | |
fi; | |
## Check if the amount is empty | |
if [ -z "$AMOUNT" ]; then | |
throw "$FUNCTION: Empty amount."; | |
return 1; | |
fi; | |
## Check if the variable is undefined | |
if ! is_defined "$VARIABLE"; then | |
throw "$FUNCTION: Undefined variable: $VARIABLE"; | |
return 1; | |
fi; | |
## Add the amount to the variable | |
eval "$VARIABLE=\$((\$$VARIABLE + \$AMOUNT));" | |
}; | |
increment() { FUNCTION="increment()"; | |
## Handle options | |
case "$1" in | |
-h) | |
echo; | |
echo "$FUNCTION: increment a variable by one"; | |
echo; | |
echo ' usage: increment -h'; | |
echo ' usage: increment [--] variable'; | |
echo; | |
return 1; | |
;; | |
--) shift; ;; | |
-*) throw "$FUNCTION: Invalid option: $1"; return 1; ;; | |
esac; | |
## Get the arguments | |
VARIABLE="$1"; | |
## Add one to the variable, let plus_equals() error handle | |
plus_equals "$VARIABLE" 1; | |
}; | |
decrement() { FUNCTION="decrement()"; | |
## Handle options | |
case "$1" in | |
-h) | |
echo; | |
echo "$FUNCTION: decrement a variable by one"; | |
echo; | |
echo ' usage: decrement -h'; | |
echo ' usage: decrement [--] variable'; | |
echo; | |
return 1; | |
;; | |
--) shift; ;; | |
-*) throw "$FUNCTION: Invalid option: $1"; return 1; ;; | |
esac; | |
## Get the arguments | |
VARIABLE="$1"; | |
## Subtract one from the variable, let plus_equals() error handle | |
plus_equals "$VARIABLE" -1; | |
}; | |
is_defined() { FUNCTION="is_defined()"; | |
## Handle options | |
case "$1" in | |
-h) | |
echo; | |
echo "$FUNCTION: check if a variable is defined"; | |
echo; | |
echo ' usage: is_defined -h'; | |
echo ' usage: is_defined [--] variable'; | |
echo; | |
return 1; | |
;; | |
--) shift; ;; | |
-*) throw "$FUNCTION: Invalid option: $1"; return 1; ;; | |
esac; | |
## Get the arguments | |
VARIABLE="$1"; | |
## Check if the variable is empty | |
if [ -z "$VARIABLE" ]; then | |
throw "$FUNCTION: Empty variable name."; | |
return 1; | |
fi; | |
## Check if the variable is defined | |
[ -n "$(eval echo "\$$VARIABLE")" ]; | |
}; | |
std_HARDFAIL=true; | |
throw() { FUNCTION="throw()"; | |
## Internal variables | |
FORCEFAIL=false; | |
FORCELIVE=false; | |
## Handle options | |
## Ignore shellcheck warnings about single-quote expansions | |
# shellcheck disable=SC2016 | |
case "$1" in | |
-h) | |
echo; | |
echo "$FUNCTION: Throw an error"; | |
echo; | |
echo ' if $std_HARDFAIL is true, the script will exit'; | |
echo ' if the first argument is -f, the script will exit (regardless of $std_HARDFAIL)'; | |
echo ' if the first argument is -l, the script will NOT exit (regardless of $std_HARDFAIL)'; | |
echo; | |
echo ' usage: throw -h'; | |
echo ' usage: throw [-f] [error ...]'; | |
echo ' usage: throw [-l] [error ...]'; | |
echo; | |
return 1; ;; | |
-f) | |
FORCELIVE=false; | |
FORCEFAIL=true; | |
shift; ;; | |
-l) | |
FORCEFAIL=false; | |
FORCELIVE=true; | |
shift; ;; | |
--) shift; ;; | |
-*) throw "$FUNCTION: Invalid option: $1"; return 1; ;; | |
esac; | |
## Get the arguments | |
ERROR="$*"; | |
## Write the error to stderr | |
echo "$ERROR" >&2; | |
## Exit the function if HARDFAIL or FORCEFAIL are set, but not if FORCELIVE is set. | |
if $std_HARDFAIL || $FORCEFAIL && ! $FORCELIVE; then | |
exit 1; | |
fi; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment