I have to work with KSH (yeah that hell shell). I need to use a fork, a subroutine as following:
#!/bin/ksh
PIPE=PIPE_$$
PIPE_ERR=PIPE_ERR_$$
export TEST_FILS
$(. ./LanceFils.ksh 2>${PIPE_ERR} 1>${PIPE}) &
PID_CHILD=$!
echo "Nom du fichier PIPE: ${PIPE}"
echo "Processus fils : " $!
wait ${PID_CHILD}
echo "Code retour: " $?
echo "Sortie standard de PROC_FILS : " $(cat ${PIPE})
echo "Sortie d'erreur(s) de PROC_FILS : " $(cat ${PIPE_ERR})
echo "Contenu de TEST_FILS: ${TEST_FILS}"
rm -rf ${PIPE}
Content of LanceFils.ksh
#!/bin/ksh
TIMEOUT=5
export TEST_FILS
echo "Je suis le script fils et j'attends ${TIMEOUT} secondes"
echo "Nom du pipe du pere ${PIPE}"
sleep ${TIMEOUT}
TEST_FILS="Je suis le fils"
echo "Salut c'était bien !!!"
exit 10
I know this do not work, btw i try to find a way to make it works ... In my code you can see i want share TEST_FILS variable between child and parent. There is way in KSH to share a variable, as in perl by using "share" or if i have to use a pipe like in C ?
Thank you.
Variables can be passed from a parent shell to child shell, not the other way around. However, you can do a workaround if you are really in need of the value set in the child shell.
One possible workaround: In the child shell, write the env variables of your interest along with their values to a file. Once you are back in the parent shell, run this file so that the env variables needed are overwritten with what is set in the file. You can refer here for an example.
A shell variable is just a piece of memory inside the running shell process. An Environment Variable is a variable that the shell copies into its environment prior to calling another program.
To understand the limitations of Environment Variables, one must understand the concept of the environment of a process: It's a single section of memory that is not shared between processes, but passed on from one process to another during an
exec
system call. (See also: Environment Variables)The environment gets passed on from one process to the next, and subsequent processes can change it prior to calling another program.
This environment also has certain restrictions: It can consist only of a list of nul-terminated strings, terminated by a null-pointer. Essentially, it's an array of strings.
Note that these strings do not need to follow the
VARNAME=value
structure, which is a convention adopted by early unix shells.ksh93
does support shell co-processes, though.After starting a child process, the parent can connect to the child's stdin and stdout with the
<& p
and>& p
redirection operators. It can be a challenge to handle these correctly, as many operations close their stdout on termination, which will close the pipe to the child.