Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make array available to parent script

Status
Not open for further replies.

warburp

IS-IT--Management
Oct 27, 2003
44
GB
Hi

I have a ksh script that looks for a child pid using a function within the script. I cannot seem to pick up the array, so I am presuming that the function runs in another shell and loses the array when it finishes.

1. Can anyone confirm this is the situation
2. Can anyone tell me how I can make the array available to the parent script?

Thanks

Phil.
 
Please post the scripts ( or at least the relevant parts)

Dickie Bird (:)-)))
 
The script (or most of it):

AWK=nawk
PS="ps -eaf -o pid,ppid,user,args"


#-----------------------------------------------------------
#Function to find the child processes and display them
#-----------------------------------------------------------
function childpid {

set -A childarray #Null the array
pid=$1
processes=`eval $PS | grep $pid | grep -v $$ | $AWK '{ if ($2 == p) print }' p=$pid` # if the 2ndd field = pid then print the ps
if [ "$processes" ] # if processes is not null
then
echo "$processes" | while read process # echo the process to the screen and read each line
do

child1=`echo "$process" | $AWK '{ print $1 }'` # print the first field
child2=`echo "$process" | $AWK '{ print $4 }'` # print the fourth field
echo "\t\t$child1\t\t $child2" # print the child process
pid=`echo "$process" | $AWK '{ print $1 }'`
#echo "$ARR2"
childarray[$ARR2]=$pid # NO spaces
#echo $pid # for debug
#echo "${childarray[$ARR2]} Array number" # for debug
ARR2=$(($ARR2+1))

childpid $pid # find the child of the process

done
fi

}


#-----------------------------------------------------------
#Trap any errors
#-----------------------------------------------------------

trap "echo 'Control-C or Control-D cannot be used' ; sleep 1 ; clear ; continue " 1 2 3
while true
do
clear
set -A pidarray
set -A childarray
ARR=0
pid=0


#-----------------------------------------------------------
#Load the PID numbers from the file into an array
#-----------------------------------------------------------

set -A pidarray #Null the array
cat $DIRTEMP$PIDFILE | while read LINE #read each line in the file

do
pidarray[$ARR]=$LINE # NO spaces
#echo $LINE # for debug
#echo "${pidarray[$ARR]} Array number" # for debug
ARR=$(($ARR+1))
done

#-----------------------------------------------------------
#First Menu of Choices
#-----------------------------------------------------------

echo "TRW Automotive Aftermarket: `hostname`\t\t User:$FULLNAME\n"
echo "\t MLE UTILITIES MENU - KILL MLE SCRIPTS - $SYSTEM
\t ------------------------------------"

NUMPIDS=0
i=0
processes=""

NUMPIDS=${#pidarray[*]} #count the number of arrays
#echo "$NUMPIDS Number of arrays" # for debug


echo "\t \t`ps -p 999999999999999999 -o pid,user,time,args`\n" # set up header

while (( i < ${NUMPIDS} )) #while the array count < i
do
pid=${pidarray}
#echo &quot;$pid&quot; #for debug
echo &quot;\t $i -- \t`ps -p $pid -o pid,user,time,args | tail -1`&quot;

processes=&quot;start&quot;
while [ &quot;$processes&quot; ]
do
echo &quot;\t\t------------------ child processes ------------------&quot;
childpid $pid # find the child processes
done

echo &quot;\n&quot;

((i+=1))

done
 
I do this for my own use:
Code:
ps -fe | awk 'BEGIN{pid='$PidSearched'}
function child(id  ,i){
 print line[id];if(id==pid)printf &quot;\n&quot;
 for(i in ppid) if(ppid[i]==id) child(i)
}
function parent(id  ,i){
 i=ppid[id]
 if(line[i]>&quot;&quot; && i!=id) buf=parent(i)&quot;\n&quot;line[i]
 return buf
}
{ppid[$2]=$3;line[$2]=$0}
END{print line[&quot;PID&quot;]&quot;\n&quot;parent(pid)&quot;\n&quot;;child(pid)}
'
You can adapt this awk script to meat your requirement.

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top