本文介绍了无限while循环在bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我真的很挣扎,看看为什么这个while循环永远不会结束,在循环开始的时候,我的变量LOC设置为测试/,这是我创造来测试这个程序的路径,它具有以下布局:结果我的测试目录的布局结果I'm really struggling to see why this while-loop never ends, when the loop starts, my variable LOC is set to Testing/, which is a directory I created to test this program, it has the following layout:我希望循环结束后所有目录有适用于他们的计数功能。结果下面是我试过的事情;I want the loop to end once all Directories have had the "count" function applied to them.Here are the things I have tried;我检查我的计数功能,它不会产生无限循环I've checked my count function, and it doesn't produce an infinite loop我试着通过手工算法PARSE=1LOC=$LOC/countAVAILABLEDIR=$(ls $LOC -AFl | sed "1 d" | grep "/$" | awk '{ print $9 }')while [ $PARSE = "1" ]doif [[ ${AVAILABLEDIR[@]} == '' ]]; then PARSE=0fiDIRBASE=$LOCfor a in ${AVAILABLEDIR[@]}; doLOC="${DIRBASE}${a}"LOCLIST="$LOCLIST $LOC"countdonefor a in ${LOCLIST[@]}; doTMPAVAILABLEDIR=$(ls $a -AFl | sed "1 d" | grep "/$" | awk '{ print $9 }')PREPEND=$aif [[ ${TMPAVAILABLEDIR[@]} == '' ]]; then continuefifor a in ${TMPAVAILABLEDIR[@]}; do TMPAVAILABLEDIR2="$TMPAVAILABLEDIR2 ${PREPEND[@]}${a}"doneNEWAVAILABLEDIR="$NEWAVAILABLEDIR $TMPAVAILABLEDIR2"doneAVAILABLEDIR=$NEWAVAILABLEDIRNEWAVAILABLEDIR=''LOC=''done我真的很挣扎,任何投入将大大AP preciated,我一直在试图找出这一点在过去的几个小时。I am really struggling, and any input would be greatly appreciated, I've been trying to figure this out for the last couple of hours.推荐答案您应该尝试运行带有参数-x脚本,或将其写入第一行:You should try to run the script with argument -x, or write it into the first line:#!/bin/bash -x然后,它会告诉你一切它。Then it tells you everything it does.在这种情况下,您可能会注意到两个错误:In that case, you might notice two errors: 您不会被重置TMPAVAILABLEDIR2You never reset TMPAVAILABLEDIR2您在常规文件上做LS为好。You do ls on regular files as well. 这篇关于无限while循环在bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-06 05:07