The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 01-06-2009
philp philp is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 2
sh recursive sub-shell exit status problem

Hi
I am trying to detect the exit status of a recursive function which I am running as a sub-shell. Below I have listed part of the shell script file that I am running...sorry its still a little long winded:-

Code:
#!/bin/sh

addFile()
{
  fileName="${1##*/}"           # Extract filename from path.
  if [ $fileName == "phil" ]
  then
    return 1 #Kick off error...
  else
    return 0
  fi
}

addCommitFiles()
{
  # Traverse to database directory or below
  cd "$topDir/$1"
  if [ $? != 0 ]
  then
    echo "topDir error $topDir/$1"
    exit 1 # Treat as fatal error.
  fi

  # List all files/dirs in this directory.
  ls | while read i
  do
    # Check if directory.
    if [ -d "$i" ]
    then
      # Pass in full relative path to topDir.
      ( addCommitFiles "$1/$i" )
      ret1=$?   # This is never non-zero???
      echo "RET=$ret1 != 1"
      if [ $ret1 != 0 ]
      then
        echo "ERR2???"
        exit 1 # Treat as fatal error.
      fi
      echo "ERR3"
    else
        echo "${rootDir}/$1/${i}"
        addFile "$1/$i"
        if [ $? != 0 ]
        then
          echo "ERR1"
          exit 1 # We get this error and would exoect 
        fi
    fi
  done
  exit 0
}

  # Main
  usrNam="XXX"
  topDir=`pwd`
  rootDir="./"

  (addCommitFiles "./$usrNam")
  if [ $? != 0 ]
  then
    echo "ERROR"
  else
    echo "OK"
  fi
I have created the following directory structure in my local directory:-
XXX
files x y z
dir=YYY
files x1 y1 z1
dir=ZZZ
files phil x2 y2 z2

When I run the shell-script I get the output:-
.//./XXX/a
.//./XXX/b
.//./XXX/c
.//./XXX/YYY/x1
.//./XXX/YYY/y1
.//./XXX/YYY/z1
.//./XXX/YYY/ZZZ/phil
ERR1
RET=0 != 1
ERR3
RET=0 != 1
ERR3
OK

What I dont understand is why I do not get the "ERR2???". I get ERR1 out because addFile returns 1, but I was hoping to get ERR2???" out because the subshell then exits with exit 1.
I obviously am missing something here and would be grateful if anyone can help?

Thanks

Last edited by philp; 01-07-2009 at 05:58 AM.. Reason: Added Code tags