oe-init-build-envshell
#!/usr/sh if [ -z "$ZSH_NAME" ] && [ "x$0" = "x./oe-init-build-env" ]; then echo "Error: This script needs to be sourced. Please run as '. ./oe-init-build-env'" else if [ -n "$BASH_SOURCE" ]; then OEROOT="`dirname $BASH_SOURCE`" else OEROOT="`pwd`" fi OEROOT=`readlink -f "$OEROOT"` export OEROOT . $OEROOT/scripts/oe-buildenv-internal && \ $OEROOT/scripts/oe-setup-builddir && \ [ -n "$BUILDDIR" ] && cd $BUILDDIR unset OEROOT unset BBPATH fi
第一步,判斷該腳本是不是用source或者.的方法運行的。 可是ui
[ "x$0" = "x./oe-init-build-env" ]只能對./oe-init-build-env這種執行方式報錯,對./yocto/oe-init-build-env是不會報錯的。
第二步,設置OEROOT這個變量。當用.或者source去執行腳本時,BASH_SOURCE這個變量會被自動設置到源文件路徑。因而dirname $BASH_SOURCE就得到了腳本所在目錄。readlink -f $OEROOT得到了絕對路徑。code
第三步,執行oe-buildenv-internal和oe-setup-builddir這兩個腳本,而且進入到build目錄。ip
(注意這裏的oe-buildenv-internal是用.執行的,而oe-setup-build是fork shell執行的。由於oe-setup-builddir中用到了OEROOT這個變量,因此在此以前,OEROOT必須被export,正如腳本中所作的那樣。)it
第四步,unset一些變量。由於.或者source的執行方式是在原shell中執行,並不fork shell,因此若是不unset,會一直留在該shell中。class
chenqi@chenqi-laptop ~/MyPro/ShellScript/yocto $ . ./oe-init-build-env
BASH_SOURCE = ./oe-init-build-env, OEROOT = .
OEROOT = /home/chenqi/MyPro/ShellScript/yocto變量
chenqi@chenqi-laptop ~/MyPro/ShellScript $ source yocto/oe-init-build-env
BASH_SOURCE = yocto/oe-init-build-env, OEROOT = yocto
OEROOT = /home/chenqi/MyPro/ShellScript/yocto方法
可見,不管在哪一個目錄下執行,最後得到的OEROOT的絕對路徑都是一致的。(主要利用BASH_SOURCE, dirname, readlink)。腳本