下面是一个 hack,但是......好吧,这是众所周知的。:)
让我们从设置测试数据集开始:
for f in 1_{dog_{yorkshire,"golden retriever"},cat_{persian,siamese}}.sh \
2_spider_tarantula.sh; do
echo "$f" >"$f"
done
然后我们可以为每个文件建立一个环境变量,其中包含一个内容数组:
# encode name to be a valid shell variable
translate_name() {
local -a components
local val retval
IFS=_ read -r -a components <<<"$1"
for component in "${components[@]}"; do
val=$(printf '%s' "$component" | base64 - -)
val_eqs=${val//[!=]/}
val_eqs_count=${#val_eqs}
val_no_eqs=${val//=/}
printf -v retval '%s%s_%s__' "$retval" "$val_no_eqs" "$val_eqs_count"
done
printf '%s\n' "${retval%__}"
}
for f in *.sh; do
varname=$(translate_name "${f%.sh}")
mapfile -t "CONTENT_$varname" <"$f"
done
那么,假设您想要遍历子树。
您可以列出与该子树关联的数组变量:
get_subtree_vars() {
local subst varname
varname=CONTENT_$(IFS=_; translate_name "$*")
printf -v subst '"${!'"$varname"'@}"'
eval 'printf "%s\n" "'"$subst"'"'
}
...并将它们转换回密钥:
# given an encoded variable name, return its original name
# inverse of translate_name
get_name() {
local varname section
local -a sections
for varname; do
retval=
varname=${varname#CONTENT_}
varname=${varname//__/ }
IFS=' ' read -r -a sections <<<"$varname"
for section in "${sections[@]}"; do
val_eqs_count=${section##*_}
val_no_eqs=${section%_*}
val=$val_no_eqs
for (( i=0; i<val_eqs_count; i++ )); do
val+="="
done
retval+=$(base64 -D - - <<<"$val")_
done
printf '%s\n' "${retval%_}"
done
}
...并检索它们的值:
# given an encoded name, retrieve a NUL-delimited list of values stored
# this could be done much more safely with bash 4.3+ using namerefs
get_values() {
local name cmd
local -a values
for name; do
[[ $name = CONTENT_* ]] || name=CONTENT_$name
printf -v cmd 'values=( "${%q[@]}" )' "$name" && eval "$cmd"
printf '%s\0' "${values[@]}"
done
}
# given a name, call a function for each leaf value associated
call_for_each() {
local funcname=$1; shift
while IFS= read -u 3 -r subtree_var; do
while IFS= read -u 4 -r -d '' value; do
"$funcname" "$value"
done 4< <(get_values "$subtree_var")
done 3< <(get_subtree_vars "$@")
}
因此:
printfunc() { printf '%q\n' "$@"; }
call_for_each printfunc 1 cat
...将发出:
1_cat_siamese.sh
1_cat_persian.sh
值得注意的是,这些是数据,而不是元数据——注意 .sh 扩展名,我们在创建时从变量中删除了它!
另请注意:由于eval
使用 base64 编码来清理文件名中可能存在的任何尝试的 shell 转义,因此上述代码中的使用应该可以避免转义尝试(以及因此通过恶意文件名进行的 shell 注入攻击);使用提供了printf %q
一个额外的层。在不存在这些保证的任何情况下,请谨慎部署上述方法。
综上所述——通过将内容读入内存,上述内容使事情变得非常复杂。考虑以下自包含代码作为上述示例的替代方案:
get_subtree_files() {
local prefix
local -a files
prefix=$(IFS=_; printf '%s\n' "$*")
files=( "$prefix"* )
# note that the test only checks the first entry of the array
# ...but that's good enough to detect the no-matches case.
[[ -e $files ]] && printf '%s\0' "${files[@]}"
}
xargs -0 cat < <(get_subtree_files 1 cat)