bash - 每个命令 "Argument list too long"

标签 bash list arguments

<分区>

偶尔,当我有一个生成大数组的程序时,我会遇到这个错误,每个命令都会抛出错误 “参数列表太长” 即使我只是输入:

$ cp
-bash: /bin/cp: Argument list too long
$

我不能使用 ls,甚至不能用 vim 打开一个新文件:

$ vim test.txt
-bash: /usr/bin/vim: Argument list too long
$

我尝试使用“等待”让所有 bg 进程完成,但没有任何变化。它似乎不一致地发生,但当它发生时,唯一的解决办法是重新启动 shell。

任何想法可能会发生什么?

更新:我做了一些进一步的测试,我得到了可重复的错误。当递归定义的数组长度达到 85 个元素时,就会发生这种情况。抛出错误的第一个命令是 bc,它甚至不依赖于数组!然后从那里开始,几乎所有其他命令都会抛出相同的错误。

更新:我正在使用的程序有很多 bash 脚本一起工作,但我确定问题总是出现在这个脚本中:

function MPMDrun_prop()
{
PARDIR=$1
COMPDIR=$2
runSTR=$3
NUMNODES=$4
ForceRun=$5

if [ $# -le 3 ] ; then
echo "USAGE: MPMDrun_prop \$PARDIR \$COMPDIR \$runSTR \$NUMNODES \$ForceRun"
fi
echo "in MPMDrun_Prop"
. $PARDIR/ParameterScan.inp
. $MCTDHBDIR/Scripts/get_NumberOfJobs.sh

if [ "$MPMD" != "T" ]; then
  MPMDnodes=1
fi

## If no runscripts in the $PARDIR, copy one and strip of the line which runs the program
if [ -z "$(ls $PARDIR/run*.sh 2> /dev/null)"  ] ; then

 if [ "$forhost" == "maia" ]; then
   cp $MCTDHBDIR/../PBS_Scripts/run-example-maia.sh $PARDIR/run.tmp
   sed 's|mpirun.*||' < $PARDIR/run.tmp > $PARDIR/run.sh
   jobtime=86400
 elif [ "$forhost" == "hermit" ]; then
   cp $MCTDHBDIR/../PBS_Scripts/run-example-hermit.sh $PARDIR/run.tmp
   sed 's|aprun.*||' < $PARDIR/run.tmp > $PARDIR/run.sh
   jobtime=86400
 elif [ "$forhost" == "hornet" ]; then
   cp $MCTDHBDIR/../PBS_Scripts/run-example-hornet.sh $PARDIR/run.tmp
   sed 's|aprun.*||' < $PARDIR/run.tmp > $PARDIR/run.sh
   jobtime=86400
 elif [ "$forhost" == "bwgrid" ]; then
   cp $MCTDHBDIR/../PBS_Scripts/run-example-BWGRID.sh $PARDIR/run.tmp
   sed 's|mpirun.*||' < $PARDIR/run.tmp > $PARDIR/run.sh
   jobtime=86400
 fi
 sed 's|nodes=[0-9]*|nodes=0|' < $PARDIR/run.sh > $PARDIR/run.tmp
 sed 's|#PBS -N.*|#PBS -N MONSTER_'$MonsterName'|' < $PARDIR/run.tmp > $PARDIR/run.sh_

 rm $PARDIR/run.sh
 rm $PARDIR/run.tmp
 chmod 755 $PARDIR/run.sh_
 echo ". $MCTDHBDIR/Scripts/RunFlagSleeper.sh" >> $PARDIR/run.sh_
 ## Include check_convergence.sh for mixed relax/prop compatibility
 echo ". $MCTDHBDIR/Scripts/check_convergence.sh" >> $PARDIR/run.sh_
 echo "RunFlagSleeper $jobtime " >> $PARDIR/run.sh_
 echo "(" >> $PARDIR/run.sh_
 cp $PARDIR/run.sh_ $PARDIR/run1.sh
fi
### Add $runSTR to the most recent runscript
### find runscript$N.sh (run1.sh, run 2.sh, etc)  that has numnodes less than $MPMDnodes
for qq in $(ls $PARDIR/run[0-9]*.sh | sort -g ); do
  NodesInRun=$(cat $qq | grep -o "nodes *= *[0-9]*" | grep -o "[0-9]*")
  if [ "$NodesInRun" -lt "$MPMDnodes"  ]; then
   ## The number of nodes already specified in the runscript doesnt exceed the maximum, so add on another job
   NewNodes=$(echo "$NodesInRun+$NUMNODES" | bc)
   ## Start each aprun command in its own subshell
   ## wait for 24 hrs after aprun, to guarantee that no subshell finishes before the job is done
   sed 's|nodes=[0-9]*|nodes='$NewNodes'|' < $qq > $qq-1
   sed 's|\(RunFlagSleeper .*\)|\1 '$COMPDIR'|' <$qq-1 >$qq
   rm $qq-1
     echo "  (" >> $qq
     ## Sleeps for $jobtime - 5 mins, then removes runflag. in case aprun doesnt finish in $jobtime 
     echo "    cd $COMPDIR" >> $qq
     echo "    $runSTR" >> $qq
     ## remove runflag after aprun command has finished
     echo "    rm $COMPDIR/RunFlag" >> $qq
#     echo "sleep $jobtime" >> $qq-1
     echo "  ) &" >> $qq
#   mv $qq-1 $qq
   ## put a flag in the computation directory so it isnt computed multiple times
   touch $COMPDIR/RunFlag

   if [[ "$NewNodes" -ge "$MPMDnodes" || "$ForceRun" == "T"  ]]; then
    ## This last process made the nodecount exceed the maximum, or there is a ForceRun flag passed
    ## So now, exceute the runscript and start another
    echo "  wait" >> $qq
    echo ") &" >> $qq
    echo "PID=\$!" >> $qq
    echo "wait \$PID" >> $qq
    ## Ensure the queue has room for the next job, if not, wait for it
   Njobs=$(get_NumberOfJobs $runhost)
   while [ "$Njobs" -ge "$maxjobs" ]; do
    echo "Njobs=$Njobs and maxjobs=$maxjobs"
    echo "Waiting 30 minutes for que to clear"
    sleep 1800
   done
    echo "qsub $qq"
#    qsub $qq
    RunCount=$(echo $qq | grep -o 'run[0-9]*.sh' | grep -o '[0-9]*')
    let "RunCount++"
    cp $PARDIR/run.sh_ $PARDIR/run$RunCount.sh
   fi
  fi
done
} 

错误通常在第一个 cpbc 处第 80-90 次调用此函数时开始。我已经评论了所有数组操作,所以这是由数组太大引起的可能性为零。环境保持在 ~100-200 Kb,所以这也不是问题。

最佳答案

该错误消息有点误导。它应该说类似“参数列表和环境使用了太多空间”之类的内容。

环境包含您导出的所有环境变量,以及您的 shell 启动时使用的环境。通常,环境应该只有几千字节,但没有什么可以阻止您 export处理一个百万字节的字符串,如果这样做,您将用完所有允许的空间。

系统允许参数 + 环境有多少空间并不完全明显。您应该能够使用 getconf ARG_MAX 查询限制, 以及 Gnu xargs您可以从 xargs --show-limits </dev/null 获得更多信息(在这两种情况下,假设您没有超过限制:)),但有时实际可用空间会小于指示的空间。

无论如何,尝试将兆字节填充到环境中并不是一个好主意。如果您想这样做,请将数据放在一个临时文件中,然后只导出文件名。

关于bash - 每个命令 "Argument list too long",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26268969/

相关文章:

c++ - 延迟文件数据几分钟

bash - 如何从 bash 中的特定应用程序数据库文件创建日志文件?

bash - linux环境变量设置消失

python - 在 Python 中编辑列表内容

python - 在 Django 中为多个查询编写 View 的最佳方法?

javascript - 为什么 JavaScript 参数有 length 属性而其他对象没有?

linux - 以 root 身份获取 Firefox 版本

java - 使用 Java 7 查找与属性值匹配的唯一对象

c++ - 处理 char ** argv

python - 如何在argparse中将store_true和值存储在互斥组中?