当前Linux发行版中的XMLStarlet版本每次xmlstarlet ed
调用的操作限制为128次,并且所有版本都受到操作系统的最大命令行长度的限制。如何解决这一问题?
以下内容将长的xmlstarlet编辑列表分解为较短操作的管道:
xmlstarlet_max_commands=100 # max per instance; see http://sourceforge.net/tracker/?func=detail&aid=3488240&group_id=66612&atid=515106
shopt -s extglob # enable +([0-9]) as an equivalent to the regex ^[[:digit:]]+
xmlstarlet_ed() {
declare -a global_parameters
declare -a parameters
declare -i num_commands
declare -i cmd_len
global_parameters=( )
parameters=( )
num_commands=0
global_parameters_remaining=$1; shift
while (( global_parameters_remaining )); do
global_parameters+=( "$1" ); shift
(( global_parameters_remaining-- ))
done
while (( "$#" )) ; do
cmd_len=$1; shift
if ! [[ $cmd_len = +([0-9]) ]] ; then
echo "ERROR: xmlstarlet_ed commands must be prefixed by run length"
return 1
fi
if (( num_commands < xmlstarlet_max_commands )) ; then
parameters+=( "${@:1:$cmd_len}" )
num_commands+=1
shift $cmd_len
else
xmlstarlet ed "${#global_parameters[@]}" "${global_parameters[@]}" "${parameters[@]}"
| xmlstarlet_ed "${#global_parameters[@]}" "${global_parameters[@]}" "$cmd_len" "$@"
return 0
fi
done
if (( ${#parameters[@]} > 0 )) ; then
xmlstarlet ed "${global_parameters[@]}" "${parameters[@]}"
else
cat
fi
}
它可以这样调用:
# first list passed is global parameters; first the count, then the values
# pass only a 0 if no global parameters are desired
global_parameters=( 2 -N "xhtml=http://www.w3.org/1999/xhtml" )
# build up the parameter list as length/command pairs; the lengths are used
# to determine the potential split points between subprocesses
parameters=( )
while read; do
parameters+=( 8 -s /xhtml:html/xhtml:body -t elem -n line -v "$REPLY" )
done
# ...and actually invoke:
xmlstarlet_ed "${global_parameters[@]}" "${parameters[@]}"
<<<"<html xmlns='http://www.w3.org/1999/xhtml'><body/></html>"