help with counting processes, bizzare behavior

I have a ksh script (dtksh Version M-12/28/93d on Solaris 10) that is run daily by cron and sometime hangs forever. I need to detect if there is an old copy hung before I start the new run, and if so send an email and exit the script. Here is part of the code:

#!/usr/dt/bin/dtksh
PROGNAME=$(basename $0)
req_ver=93d
if [[  ! ${.sh.version} || (${.sh.version##*/} < $req_ver) ]] ; then 
    print "$PROGNAME requires ksh $req_ver or higher" ; exit 1 ; 
fi

count=$(pgrep -z global $PROGNAME | wc -l | awk '{print $1}')
if [[  $count -gt 1 ]] ; then 
   mailx -s "Houston we have a problem" ...
   exit
fi

Here is the problem, and I am really stumped:
Even when this script is the only instance, $count still equals 2.

Debugging shows that:
pgrep emits one PID
wc -l returns " 1 " (Seven spaces, the digit 1, single space. I need to trim the spaces before the test, thus the awk command.)
and then awk returns 2

??? !!! ???

I see the same thing when using these versions of the command:

count=$(pgrep -z global $PROGNAME | wc -l | tr -d [:blank:])
count=$(ps -z global | grep $PROGNAME | grep -v grep | wc -l | awk '{print $1}')

When I test at the shell using these checks against existing processes the result is alway correct. I have checked and double checked and it seems that the digit 1 surrounded by spaces is being piped into either tr or awk, and both return the digit 2 with no spaces.

Help will be really appreciated.

I guess following should be able to give the count of running $PROGNAME instances. Is it not?

count=$(pgrep -z global $PROGNAME | wc -l)

OR

 
count=$(ps -z global | grep $PROGNAME | grep -v grep | wc -l)

I would expect that when the script runs:

count=$(pgrep -z global $PROGNAME | wc -l | awk '{print $1}')

The script will create a child process (thats what the round brackets do) so that it can run the pgrep, wc, awk, etc. When it creates a child process, it will create it with the same name as the parent process, ie $PROGNAME. Therefore you will always have more than one process...

Both of these set count to 1 and not to 2, regardless that they run in a subshell:

count=$(pgrep -z global $PROGNAME | wc -l)
count=$(ps -z global | grep $PROGNAME | grep -v grep | wc -l)

It is only when I add the pipe to tr or awk that I get '2'

count=$(pgrep -z global $PROGNAME | wc -l | tr -d [:blank:])
count=$(ps -z global | grep $PROGNAME | grep -v grep | wc -l | awk '{print $1}')

---------- Post updated at 05:49 PM ---------- Previous update was at 01:46 PM ----------

I learned something but I don't understand it. Anyone explanation would be really appreciated.
I took the existing code and removed the parens, getting rid of the subshell issue, if any. Then I added a tee to each part of the pipeline, like in each of these:

pgrep -l -x -z global $PROGNAME | tee n1 | wc -l | tee n2 | awk '{print $1}'

ps -z global -e -f | grep $PROGNAME | grep -v grep | tee n3 | wc -l | tee n4 | awk '{print $1}'

Looking at the files n1, n2, n3, and n4 shows the following:

1) When I remove the first line "#!/usr/dt/bin/dtksh" and just called the executed the script by name from the command line it actually ran, and all ps commands made no matches.

2) When I remove the first line "#!/usr/dt/bin/dtksh" and sourced the script from the command line like so: " . /mnt/cplog/scripts/check_portals" it ran and matched two instances of /usr/dt/bin/dtksh.

3) When I restore the first line "#!/usr/dt/bin/dtksh" and ran the script normally it matched two copies of itself, one the parent of the other:
root 9143 9127 0 23:41:00 pts/1 0:00 /usr/dt/bin/dtksh /mnt/cplog/scripts/check_portals
root 9127 9105 0 23:40:59 pts/1 0:00 /usr/dt/bin/dtksh /mnt/cplog/scripts/check_portals

This was without the subshell-creating expressions "$(...). Why does running a script create these two instances?

I thought it was perhaps because I executed the script from the dtksh shell, and then the script forked and exec'd another dtksh shell to run in, but when I tested that theory by calling the script from tcsh instead of dtksh I got the same thing. Any ideas?

I thought I understood ksh pretty will but now I am confused.

ksh generally invokes sub shells for pipes too, this is why /var/run/*.pid files are so popular for this sort of thing.

If you really want to grep the ps output, perhaps grep -v $$