Pass arguments to function as environment variable MO_ARGS

This commit is contained in:
Andrea Bonomi 2019-08-07 14:59:00 +02:00
parent eac2685632
commit fcedd32155
4 changed files with 17 additions and 4 deletions

10
mo
View File

@ -62,6 +62,8 @@
# options and arguments. This puts the content from the
# template directly into an eval statement. Use with
# extreme care.
# MO_ARGS - Arguments passed to the function,
# only if MO_ALLOW_FUNCTION_ARGUMENTS is unset
# MO_FAIL_ON_UNSET - When set to a non-empty value, expansion of an unset
# env variable will be aborted with an error.
# MO_FALSE_IS_EMPTY - When set to a non-empty value, the string "false"
@ -156,14 +158,14 @@ mo() (
moCallFunction() {
local moArgs
moArgs=()
# shellcheck disable=SC2031
if [[ -n "${MO_ALLOW_FUNCTION_ARGUMENTS-}" ]]; then
moArgs=$3
echo -n "$2" | eval "$1" "$moArgs"
else
moTrimWhitespace moArgs "$3"
echo -n "$2" | MO_ARGS="$moArgs" eval "$1"
fi
echo -n "$2" | eval "$1" "$moArgs"
}

View File

@ -0,0 +1,3 @@
testArgs() {
echo "$MO_ARGS"
}

View File

@ -0,0 +1,4 @@
No args: [] - done
One arg: [one] - done
Multiple arguments: [aa bb cc 'x' " ! {[_.|] - done
Evil: [bla; cat /etc/issue] - done

View File

@ -0,0 +1,4 @@
No args: [{{testArgs}}] - done
One arg: [{{testArgs one}}] - done
Multiple arguments: [{{testArgs aa bb cc 'x' " ! {[_.| }}] - done
Evil: [{{testArgs bla; cat /etc/issue}}] - done