在KSH中将变量从孩子传递给父母 [英] Pass variable from a child to parent in KSH

查看:69
本文介绍了在KSH中将变量从孩子传递给父母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用KSH(是的,该死的外壳). 我需要使用fork,如下所示的子例程:

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}

LanceFils.ksh的内容

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

我知道这是行不通的,顺便说一句,我试图找到一种使之起作用的方法... 在我的代码中,您可以看到我想在子级和父级之间共享TEST_FILS变量. 在KSH中有一种共享变量的方法,例如在perl中使用"share",或者如果我必须在C语言中使用类似管道的管道?

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 ?

谢谢.

推荐答案

变量可以从父shell传递到子shell,而不能相反.但是,如果确实需要在子外壳程序中设置的值,则可以采取解决方法.

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.

一个可能的解决方法:在子外壳程序中,将所需的env变量及其值写入文件.返回父级外壳后,运行该文件,以便所需的env变量被文件中设置的值覆盖.您可以在此处引用一个例子.

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.

这篇关于在KSH中将变量从孩子传递给父母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆