这是我的问题的最简单的提要。保留的复杂性是有充分理由的,即使在这里并不明显。此外,该脚本是内部脚本,没有执行恶意代码的机会,因此评估非常完善。不需要知道它有多邪恶……;)暂时假设密钥字符串中的管道和冒号是必需的定界符。
key="target|platform|repo:revision"
hashname="hashmap"
declare -A $hashname
eval $hashname[$key]=0
echo $(eval $hashname[$key])
当然,最后两行有问题,因为eval作用在$ key变量内的管道和冒号上。我的问题是如何保护这样的字符串免受评估?我需要评估,因为我指的是哈希图的名称,而不是名称本身。预先感谢。
================================================ ==================================
好吧,我实际上开始认为我的问题不在于管道,冒号和评估。所以让我粘贴真实代码
function print_hashmap()
{
local hashname=$1
for key in `eval echo \$\{\!$hashname[@]\}`; do
local deref="$hashname[$key]"
local value=${!deref}
echo "key=${key} value=${value}"
done
}
function create_permutations2()
{
local hashname=$1 ; shift
local builds=$1 ; shift
local items=$1 ; shift
local separators=$@
echo "create_permutations(hashname=${hashname}, builds=${builds}, items=${items}, separators=${separators})"
if [ NULL != "${builds}" ]; then
for build in `echo $builds | tr ',' ' '`; do
local target="${build%%:*}"
local platforms="${build##*:}"
for platform in `echo $platforms | tr '|' ' '`; do
local key=
local ref=
if [ NULL != "${items}" ]; then
if [ NULL == "${separators}" ]; then separators=' '; fi
for separator in $separators; do
items=`echo $items | tr $separator ' '`
done
for item in $items; do
key="${target}|${platform}|${item}"
ref="$hashname[$key]"
declare "$ref"=0
done
else
key="${target}|${platform}"
ref="$hashname[$key]"
declare "$ref"=0
fi
done
done
fi
echo "created the following permutations:"
print_hashmap $hashname
}
builds="k:r|s|w,l:r|s|w"
repos="c:master,m:master"
hashname="hashmap"
declare -A $hashname
create_permutations2 $hashname $builds $repos ","
print_hashmap $hashname
我最近修改了我的代码,使其与FatalError的建议一致,使用ref代替eval,并且错误是相同的:表达式中的语法错误(靠近:master的错误标记)
也许将$key
的引号加倍?
eval $hashname["$key"]=0
echo $(eval $hashname["$key"]=0)
尝试:
eval $hashname'[$key]'=0
eval result=$hashname'[$key]'
这将$
传递到eval
,而不是首先扩展参数。
我不会讲讲eval
的弊端,但是在这种情况下,它至少增加了一些额外的复杂性。我可以想到的最简单的方法是评估字符串结果,因此要添加一些文字单引号:
eval $hashname["'"$key"'"]=0
除了$key
中的单引号外,它将防止所有内容。但是,您可以减轻头痛的事情完成相同的事情。这是我更新的脚本来说明:
key="target|platform|repo:revision"
hashname="hashmap"
declare -A $hashname
echo "With eval:"
eval $hashname["'"$key"'"]=0
eval echo \${$hashname["'"$key"'"]}
echo
echo "Without eval:"
ref="$hashname[$key]"
echo ${!ref}
declare "$ref"="1"
echo ${!ref}
注意,我更改了原始的eval
行,因为它对我来说没有意义-它试图执行结果而不是打印结果。在后面的部分中,您可以使用间接访问来访问该值,然后使用declare
对其进行分配。然后,您不必担心这些字符会被解释。
这将产生结果:
With eval:
0
Without eval:
0
1
我不确定100%为什么不起作用,但这似乎是:
builds="k:r|s|w,l:r|s|w"
repos="c:master,m:master"
hashname="hashmap"
declare -A $hashname
function print_hashmap()
{
local hashname=$1
for key in `eval echo \$\{\!$hashname[@]\}`; do
local deref="$hashname[$key]"
local value=${!deref}
echo "key=${key} value=${value}"
done
}
function create_permutations2()
{
local hashname=$1 ; shift
local builds=$1 ; shift
local items=$1 ; shift
local separators=$@
echo "create_permutations(hashname=${hashname}, builds=${builds}, items=${items}, separators=${separators})"
if [ NULL != "${builds}" ]; then
for build in `echo $builds | tr ',' ' '`; do
local target="${build%%:*}"
local platforms="${build##*:}"
for platform in `echo $platforms | tr '|' ' '`; do
local key=
if [ NULL != "${items}" ]; then
if [ NULL == "${separators}" ]; then separators=' '; fi
for separator in $separators; do
items=`echo $items | tr $separator ' '`
done
for item in $items; do
key="${target}|${platform}|${item}"
ref="$hashname[$key]"
eval $hashname[\'$key\']=0
done
else
key="${target}|${platform}"
eval $hashname[\'$key\']=0
fi
done
done
fi
echo "created the following permutations:"
print_hashmap $hashname
}
create_permutations2 $hashname $builds $repos ","
print_hashmap $hashname
对于这小部分问题:“我的问题是如何保护这样的字符串免受评估?”,我只提到bash的“参数转换”(请参见手册页),${parameter@operator}
。特别是,运算符Q评估为可以安全地用作输入的带引号的形式。
% echo ${key}
target|platform|repo:revision
% echo ${key@Q}
'target|platform|repo:revision'