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 -->
  #2 (permalink)  
Old 01-06-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,315
Quote:
Originally Posted by philp View Post
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:-

Please put code inside [code] tags. (It would be a good idea to edit your original post and add them.)
Quote:
Code:
#!/bin/sh

addFile()
{
  fileName="${1##*/}"           # Extract filename from path.
  if [ $fileName == "phil" ]

== is not a test operator in sh; use =
Quote:
Code:
  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

Thre's no need for ls, and it is the cause of your problem, since all elements of a pipeline are run in subshells. Use:

Code:
for i in *
(I'd recommend using a more descriptive variable name, e.g., file.)
Quote:
Code:
  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`

All POSIX shells have a $PWD variable; use it rather than `pwd` as command substitution is slow in all shells except ksh93.