
01-06-2009
|
|
Shell programmer, author
|
|
|
Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,315
|
|
Quote:
Originally Posted by philp
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:
(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.
|