help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] Updating global variable from recursing function.


From: Frans de Boer
Subject: [Help-bash] Updating global variable from recursing function.
Date: Sun, 01 Apr 2012 01:18:53 +0200
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.28) Gecko/20120306 SUSE/3.1.20 Thunderbird/3.1.20

The next code fragment gives me a headaches:

function mdirtree () {
#
# Copy and define some local variables.  They need to be local to allow
# recursive calling.
#
local -r sBasePath=$1
local -r sBaseURL=$2
local -r sTempFile=$(mktemp /tmp/mds.XXXXXXXX)
local    sDirBuf=""
local    sDirBufHist=""
local -i iRetValMDirNext
#
# Place the passed BasePath on the stack and get the listing
#
  pushdirtbl $sBasePath
  pushdirtbl $sBaseURL
  mkdir -p $sBasePath
  rm -f $sBasePath/.listing*
  rm -f $sBasePath/index.htm*
#
# Next loop code is to catch an error with FTP sites and/or wget.
#
  iMDirNest=$((iMDirNest+1))
  if [[ iMDirNest > iHiMDirNest ]]; then
    let iHiMDirNest=$iMDirNest
  fi
  iErrCnt=0
  while (( $TRUE )); do
    rm -f $sBasePath/.listing*
nice -n $iNiceValue wget --quiet --no-remove-listing -P $sBasePath $sBaseURL/
    if [[ $? -eq 0 ]]; then
      rm -f $sBasePath/index.*
      if test -f $sBasePath/.listing; then
        break
      fi
    elif [[ iErrCnt -ne 10 ]]; then
      printf "WGET error on $sBaseURL, retrying.."
      iErrCnt=$((iErrCnt+1))
    else
      if ! test -z $fForce; then
        printf "\n\nTO MANY WGET ERRORS, ABORTING\n"
        exit 6
      else
        printf "\n\nTo MANY WGET ERRORS, SKIPPING\n"
        break
      fi
    fi
  done

  cat $sBasePath/.listing | sed -r -e 's/^dr.*/&/'  \
        -e 'T flush'    \
        -e 's/\r$//'    \
        -e 'T flush'    \
        -e 's/\.$//'    \
        -e 's/\.\.$//'    \
        -e 's/.*authors//'  \
        -e 'T fin'    \
        -e ':flush'    \
        -e 'd'      \
        -e ':fin'    \
         > $sTempFile

  if test $fDEBUG && [[ $fDEBUG -ge 5 ]]; then
    echo "DEBUG5+: ############# ORIGINAL .listing CONTENT #############"
    cat $sBasePath/.listing
    echo "DEBUG5+: ############# FILTERED BY DIRECTORIES #############"
    if test -s $sTempFile; then
      cat $sTempFile
    else
      echo "None Available"
    fi
    read -p "Enter return to continue..."
  fi
#
# Now, the temp file contains just directries or is empty.
# Check if the file is empty.
#
  if test -s $sTempFile; then
  #
  # So, the file contains one or more directory entries. Get the
  # first/next entry.  Check if it's empty, and if not recurse this
  # function to get one level deeper.  Do this for all directory entries
  # in the current temp file.
  #
    while read -u 3 sDirBuf; do
    #
    # There is (still) a directory entry to be processed in sDirBuf.
    # Get the directory entry only and strip all other info.
    #
      sDirBufHist=$sDirBuf
      sDirBuf=$(echo $sDirBuf | sed -r -e 's/^.*\ //' -e 's/\r$//')
      mdirtree $sBasePath/$sDirBuf $sBaseURL/$sDirBuf
    done 3<"${sTempFile}"
  fi

  if test $fDEBUG && [[ $fDEBUG -ge 4 ]] && [[ $iMDirNest -eq 1 ]]; then
    dispdirtbl
    read -p "DEBUG4+: Content of tDirTbl.."
  fi

  iMDirNest=$((iMDirNest-1))
  rm -f $sTempFile
}

My question revolves around the global variable 'iHiMDirNest'. When the function mdirtree() is called recursively several times, the end result is always 2 (two) and never higher even when I have seen a nesting level of 7 or higher.

Using 'bash -x ...' I can see the higher numbers, but it seems that they are not affecting the "real" global variable 'iHiMDirNest'.

Please, even if it sounds confusing, any help would be welcome.

Regards, Frans.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]