This is an example of how to use a TWO-WAY PIPE [color green]|&[/color] to allow a parent process to set the values of variables in a child process and vice-versa (This was executed on AIX)
[color red]------------------------[/color]
Parent process wants to determine the values of the variables [color green]CHILD_NUMBER[/color] and [color green]CHILD_COLOR[/color]. These variables are
set in the child process. When the parent has both of these values it tells
the child set the value of CHILD_NUMBER to the value of PAPA_NUMBER.
the parent sends the request through the pipe [color green]print -p[/color]
[color red]------------------------[/color]
the child reads the request from its stdin (which is the pipe), it then determines
what type of request..
For example
the first request sent from the parent is formated as [color green]INT#CHILD_NUMBER[/color].
The child uses [color green]'#'[/color] as a field seperator.
The first field [color green]INT[/color] tells the child to send back ONLY the
value of the second field [color green]CHILD_NUMBER[/color]
The child replys through stdout (which is the pipe) [color green]INFO#PURPLE[/color].
The parent then processes the reply simalar to the childs method by using the
first and second field.
What the values are of the first and second field and how are they are processed
can be set to anything.
[color red]-------------------------------------------[/color]
PARENT SCRIPT
[color red]-------------------------------------------[/color]
Code:
#/bin/ksh
PAPA_NUMBER=$RANDOM
echo 'Initial Values From Parent Script'
echo 'PAPA_NUMBER='$PAPA_NUMBER
echo 'CHILD_NUMBER='${CHILD_NUMBER}
echo 'CHILD_COLOR='${CHILD_COLOR}
#Start child process with two-way pipe
child |&
while [[ ${CHILD_NUMBER} != ${PAPA_NUMBER} || -z ${CHILD_COLOR} ]] do
if [[ -z ${CHILD_NUMBER} ]] then
QUESTION='CHILD_NUMBER'
echo '-----------------------------------'
echo "Ask Child value of ${QUESTION}"
print -p "INT#${QUESTION}"
elif [[ -z ${CHILD_COLOR} ]] then
QUESTION='CHILD_COLOR'
echo '-----------------------------------'
echo "Ask Child to set Parent value of ${QUESTION}"
print -p "SET#${QUESTION}"
elif [[ ${CHILD_NUMBER} != ${PAPA_NUMBER} && -n ${CHILD_NUMBER} ]] then
QUESTION='CHILD_NUMBER'
echo '-----------------------------------'
echo "Tell Child to change its value of ${QUESTION}"
echo 'PAPA_NUMBER='$PAPA_NUMBER
echo 'CHILD_NUMBER='${CHILD_NUMBER}
echo 'CHILD_COLOR='${CHILD_COLOR}
print -p CMD#CHILD_NUMBER=${PAPA_NUMBER}
echo '-----------------------------------'
echo "Ask Child value of ${QUESTION}"
print -p "INT#${QUESTION}"
fi
read -p CHILD_MSG
REPLY_TYPE=$(echo $CHILD_MSG|cut -d# -f1)
CHILD_REPLY=$(echo $CHILD_MSG|cut -d# -f2)
case $REPLY_TYPE in
INFO*) eval "${QUESTION}"'='${CHILD_REPLY};;
CMD*) eval ${CHILD_REPLY};;
esac
echo 'PAPA_NUMBER='$PAPA_NUMBER
echo 'CHILD_NUMBER='${CHILD_NUMBER}
echo 'CHILD_COLOR='${CHILD_COLOR}
done
print -p 'END'
wait
echo '-----------------------------------'
echo "Final Values From Parent "
echo 'PAPA_NUMBER='$PAPA_NUMBER
echo 'CHILD_NUMBER='$CHILD_NUMBER
echo 'CHILD_COLOR='$CHILD_COLOR
[color red]-------------------------------------------[/color]
CHILD SCRIPT
[color red]-------------------------------------------[/color]
Code:
#!/bin/ksh
CHILD_COLOR='PURPLE'
CHILD_NUMBER=$RANDOM
while [[ $PAPA_MSG != END ]] do
read PAPA_MSG
REPLY_TYPE=$(echo $PAPA_MSG|cut -d# -f1)
PAPA_REQ=$(echo $PAPA_MSG|cut -d# -f2)
case $REPLY_TYPE in
INT*) echo 'INFO#'$(eval echo \$${PAPA_REQ});;
SET*) echo 'CMD#'${PAPA_REQ}'='$(eval echo \$${PAPA_REQ});;
CMD*) eval ${PAPA_REQ};;
esac
done
JRjr
![[morning] [morning] [morning]](/data/assets/smilies/morning.gif)