get_param() {
local paramname="$1"
local result=""
local IFS="
"
for line in $SCRIPT_DATA; do
unset IFS
line=${line%%#*} # removed any comments
case "$line" in
*"$paramname"=*)
# lets check we are not matching only suffix of param
case `echo ${line%%=*}` in
$paramname) result=`eval "$line ; echo \"\\${$paramname}\""` ;;
esac
;;
esac
done
echo -n "$result"
}
local paramname="$1"
local result=""
local IFS="
"
for line in $SCRIPT_DATA; do
unset IFS
line=${line%%#*} # removed any comments
case "$line" in
*"$paramname"=*)
# lets check we are not matching only suffix of param
case `echo ${line%%=*}` in
$paramname) result=`eval "$line ; echo \"\\${$paramname}\""` ;;
esac
;;
esac
done
echo -n "$result"
}
if SCRIPT DATA is like:
param1=value1
hostname=`hostname`
paramN=valueN
get_param hostname would return the hostname of machine.
If SCRIPT_DATA is like:
hostname='`hostname`'
get_param hostname would return `hostname` avoiding expansion.
A couple of limitations:
- control structures are not regarded
- not secure (run only through trusted code)
- multi-line expressions not supported
Maybe it would have been more useful if I could split the script data to complete shell expressions but I'm not sure added complexity will be worth it either.
In any case I just put this function here to not lose it. Similar use cases pop from time to time...