linux 子進程變數傳遞給父進程

來源:互聯網
上載者:User

vim backfunc

#!/bin/sh
_CODE="comet"
_FULLBACKUP="yes"
_LOGFILE="/logs/backup/"
_DEVICE="/dev/rmt/On"
_INFORM="yes"
_PRINT_STATS="yes"

#!/bin/sh

if [ -r backfunc ];then
        .   backfunc ########################################注意:.空格 檔案名稱 或者  source backfunc

        #######################export是不行的
else
        echo "$`basename $0` cannot locate backfunc file"
fi
echo -n " Enter the code name:"
read CODE
if [ "${CODE}" != "${_CODE}" ];then
        echo  "worong code...exiting..will use defaults"
        echo $CODE
        echo $_CODE
#       exit 1
fi
echo "Ther environment config file reports"
echo "Full Backup Required :$_FULLBACKUP"
echo "The Device To Backup to is :$_DEVICE"
echo "You Are to be informed by Mail:$_INFORM"

echo "A Statistic report to be printed :$_PRINT_STATUS"

總結:sub-shell有自己的環境空間,他自己的環境中增加或修改的變數在sub-shell推出
時也隨之銷毀,不會帶回到父進程中。pipeline和(list)都會建立sub-shell,要
注意呀!

你的sh-bin中
./sh-env
一行可以改成
. sh-env

source sh-env
使他在當前shell中運行。

雖然在sh-env中export了sh_var,它的效果只會影響到sh-env本身及其子shell,你在sh-bin中的“./sh-env”這行的本質是新開一個sh來執行sh-env,結束後退回到執行sh-bin的sh中,具有遺傳性質的是前邊那個執行sh-env的sh,而不是執行sh-bin的sh
在sh-bin中不是沒有輸出sh_var,只是輸出了空值

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.