@@ 24,12 24,6 @@ pecho()
printf '%s\n' "$*"
}
-# Return the value of variable named $1
-value()
-{
- eval pecho "\"\$$1\""
-}
-
# Print all arguments to stderr and exits with status 1
die()
{
@@ 37,6 31,22 @@ die()
exit 1
}
+# Check if the variable named $1 exists
+isset()
+{
+ eval "[ \"\${$1+x}\" ]"
+}
+
+# Return the value of variable named $1, fails if no such variable exists
+value()
+{
+ match "$1" '[0-9]+' &&
+ { pecho "positional parameters can't be used with value()" >&2; return 1; }
+ isset "$1" ||
+ { pecho "$var: variable not set" >&2; return 1; }
+ eval pecho "\"\$$1\""
+}
+
# Append to variable $1, which need not exists
append()
{
@@ 68,7 78,7 @@ typecheck()
for var
do
match "$var" '[0-9]+' && die "positional parameters can't be used with typecheck"
- val=$(value "$var")
+ val=$(value "$var") || exit 1
case "$type" in
bool)
[ "$val" != true ] && [ "$val" != false ] && {
@@ 94,6 104,9 @@ typecheck()
success=false
}
;;
+ *)
+ die "$type: unknown type"
+ ;;
esac
done
$success