扩展命令时,Eval错误参数列表过长

时间:2019-05-14 22:49:41

标签: bash

我从这里获得了一些运行良好的代码,直到得到“参数列表过长”为止 我也不是开发人员,而且年龄也很大:),所以​​如果要问的不是太多,请解释一下。

有没有办法像eval那样扩展DIRCMD并同时传递每个命令,这样eval不会中断?

for (( ifl=0;ifl<$((NUMFIRSTLEVELDIRS));ifl++ )) {  FLDIR="$(get_rand_dirname)"
FLCHILDREN="";
for (( ird=0;ird<$((DIRDEPTH-1));ird++ )) {
DIRCHILDREN=""; MOREDC=0;
for ((idc=0; idc<$((MINDIRCHILDREN+RANDOM%MAXDIRCHILDREN)); idc++)) {
CDIR="$(get_rand_dirname)" ;
# make sure comma is last, so brace expansion works even for 1 element? that can mess with expansion     math, though
if [ "$DIRCHILDREN" == "" ]; then 
DIRCHILDREN="\"$CDIR\"" ;
else 
DIRCHILDREN="$DIRCHILDREN,\"$CDIR\"" ;
MOREDC=1 ;
fi
}
if [ "$MOREDC" == "1" ] ; then
if [ "$FLCHILDREN" == "" ]; then 
FLCHILDREN="{$DIRCHILDREN}" ;
else 
FLCHILDREN="$FLCHILDREN/{$DIRCHILDREN}" ;
fi
else
if [ "$FLCHILDREN" == "" ]; then 
FLCHILDREN="$DIRCHILDREN" ;
else 
FLCHILDREN="$FLCHILDREN/$DIRCHILDREN" ;
fi
fi
}
cd $OUTDIR
DIRCMD="mkdir -p $OUTDIR/\"$FLDIR\"/$FLCHILDREN"
eval "$DIRCMD"
echo "$DIRCMD"
}

我尝试回显$ DIRCMD,但是没有得到扩展的命令列表 'echo mkdir -p / mnt / nvme-test / rndpath /“ r8oF” / {“ rc”,“ XXKR”,“ p0H”} / {“ 5Dw0K”,“ oT”,“ rV”,“ coU”,“ uo“} / {” 3m5m“,” uEdA“,” w4SJp“,” 49“}'

1 个答案:

答案 0 :(得分:1)

我在遵循代码时遇到了麻烦,但是如果我对它的理解正确,那么您可以动态生成一个mkdir -p命令,并带有大括号扩展名:

'mkdir -p /mnt/nvme-test/rndpath/"r8oF"/{"rc","XXKR","p0H"}/{"5Dw0K","oT","rV","coU","uo"}/{"3m5m","uEdA","w4SJp","49"}'

由于操作系统的最大参数限制,eval失败了。

要解决此问题,您可以生成一个printf ..命令,因为这是Bash内置函数,不受参数限制,然后将其输出馈送到xargs

dircmd='printf "%s\0" /mnt/nvme-test/rndpath/"r8oF"/{"rc","XXKR","p0H"}/{"5Dw0K","oT","rV","coU","uo"}/{"3m5m","uEdA","w4SJp","49"}'
eval "$dircmd" | xargs -0 mkdir -p

如果您的xargs不支持-0,则可以改用printf "%s\n"xargs mkdir -p,尽管如果生成的名称包含空格,它的表现也不太好等等。

如果这是用于基准测试,您可能还想知道您现在可以使用xargs -0 -n 1000 -P 8 mkdir -p来并行运行8个mkdir,每个一次创建1000个Dirs。