2019独角兽企业重金招聘Python工程师标准>>>
oe-init-build-env
#!/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或者.的方法运行的。 但是
[ "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获得了绝对路径。
第三步,执行oe-buildenv-internal和oe-setup-builddir这两个脚本,并且进入到build目录。
(注意这里的oe-buildenv-internal是用.执行的,而oe-setup-build是fork shell执行的。因为oe-setup-builddir中用到了OEROOT这个变量,所以在此之前,OEROOT必须被export,正如脚本中所做的那样。)
第四步,unset一些变量。因为.或者source的执行方式是在原shell中执行,并不fork shell,所以如果不unset,会一直留在该shell中。
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)。