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



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Move Command and exit status problem visingha Shell Programming and Scripting 10 09-13-2008 08:08 PM
Move Command and exit status problem visingha UNIX for Dummies Questions & Answers 1 09-12-2008 02:14 AM
exit status running java classpath in unix shell mmcds High Level Programming 2 08-02-2007 10:06 PM
checking exit status of a shell script kdipankar Shell Programming and Scripting 2 05-09-2006 01:08 AM
Problem with exit status diganta Shell Programming and Scripting 1 09-28-2005 08:34 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #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
  #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,361
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.
  #3 (permalink)  
Old 01-07-2009
philp philp is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 2
Smile

Thanks for tour help
1) Sorry I did'nt know about the code tags...I have added them.
2) == works for me? Perhaps my version of bash excepts both???
3) Brilliant!!! Thankyou
" Thre's no need for ls, and it is the cause of your problem, since all elements of a pipeline are run in subshells."
This fixed my problem. I dont actually understand why yet, but I will have a read. Thank You!!!!
4) I will remember to use $PWD in the future. In my original code I did not need to know the current directory...this was needed for this cut down version.

Thanks Again for all your help!!!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:31 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0