最近遇到一个软件,在安装时,要求先要执行. ./profile.sh,看到这个写法,着实迷惑了下,这个和./profile.sh有什么区别?先贴出脚本的简写版本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | # Set the operating system environment variable if [ -x /bin/uname ] then OS=`/bin/uname` export OS else echo echo "What type of workstation are you using?" echo echo "Find the command uname, and insert the correct" echo "information in the .profile_nawips file." echo exit 1 fi # Initialize OS version and OS release to blank. OS_VER="" OS_REL="" case $OS in AIX ) # IBM OS_VER=`uname -v` ;; HP-UX ) # Hewlett-Packard OS="HPUX" export OS OS_VER=`uname -r | cut -f2 -d.` ;; IRIX|IRIX64 ) # Silicon Graphics OS_VER=`uname -r | cut -f1 -d.` OS_REL=`uname -r | cut -f2 -d.` # Fix for IRIX64 if [ $OS = "IRIX64" ] then OS="IRIX" export OS fi ;; Linux ) # Linux - PC OS_VER=`uname -r | cut -f1 -d.` OS_REL=`uname -r | cut -f2 -d.` if [ $OS_REL = 6 ] && [ $MACHTYPE != x86_64 ] then OS_REL=4 fi entrp=`uname -r | awk -F. '{print $NF}'| tr "[A-Z]" "[a-z]"` if [ $entrp = "el" ] || [ $entrp = "elsmp" ] then OS_REL=`echo $OS_REL`el fi ;; SunOS ) # Sun Micro Systems OS_VER=`uname -r | cut -f1 -d.` ;; * ) echo "What type of UNIX do you have?" echo echo "Find the command uname, and insert the correct" echo "information in the .profile_nawips file." ;; esac export OS_VER export OS_REL # Set NA_OS to the lowercase equivalent of OS # Add the version number to the name and for Linux, also add the release number. if [ $OS = "Linux" ] then NA_OS=`echo $OS | tr "[A-Z]" "[a-z]"``echo $OS_VER`.`echo $OS_REL` else NA_OS=`echo $OS | tr "[A-Z]" "[a-z]"``echo $OS_VER` fi export NA_OS #======================================================================== # Compilation and link flags for F77 and CC. CC="cc" export CC LD="ld" export LD AR="ar" export AR ARFLAGS="crv" export ARFLAGS RM="rm -f" export RM INCLUDES="-I$GEMINC -I$OS_INC" export INCLUDES case $OS in AIX ) CFLAGS="$INCLUDES -D$OS" LDFLAGS="-L$OS_LIB -s" ;; HPUX ) CFLAGS="$INCLUDES -D$OS +DA1.1" LDFLAGS="-L$OS_LIB +DA1.1 -s" ;; IRIX ) CFLAGS="$INCLUDES -D$OS -DUNDERSCORE -I/usr/Motif-2.1/include" LDFLAGS="-L$OS_LIB -L/usr/Motif-2.1/lib32 -s -woff 85" ;; Linux ) CC="gcc" export CC CFLAGS="$INCLUDES -D$OS -DUNDERSCORE -I/usr/X11R6/include" LDFLAGS="-L$OS_LIB -L/usr/X11R6/lib" if [ $MACHTYPE = "x86_64" ] then LDFLAGS="-L$OS_LIB -L/usr/X11R6/lib64" elif [ $NA_OS = "linux2.4" ] then LDFLAGS="$LDFLAGS -static -s" fi ;; SunOS ) CFLAGS="$INCLUDES -D$OS -DUNDERSCORE" LDFLAGS="-L$OS_LIB -s" ;; * ) echo echo "WARNING..." echo echo "No compiler options have been set." echo ;; esac export CFLAGS export LDFLAGS |
之所以贴出这么长的脚本例子,主要觉得这个脚本处理跨平台的情况很具有参考性,其实脚本就是做了个导出操作系统版本和编译选项的动作。好了,我们使用两种情况执行一下脚本,看一下情况吧,毕竟动手胜于雄辩嘛
1 2 3 4 5 6 7 8 9 | $ echo $CFLAGS $ sh ./profile.sh $ echo $CFLAGS $ . ./profile.sh $ echo $CFLAGS -I -I -DLinux -DUNDERSCORE -I/usr/X11R6/include $ vi profile.sh |
我们先使用./profile.sh的情况执行(sh ./profile.sh),然后查看环境变量,为空,然后使用. ./profile.sh的方法执行,然后再看,当前的环境变量改变了。
这个结果比较明显了,使用. ./profile.sh的方法执行脚本会等同于source profile.sh,实际上就是在当前的bash环境执行,因此会改变当前的bash环境,而使用./profile.sh和sh ./profile.sh的方法执行,我们通过命令(sh ./profile.sh)也可以猜想下:其实是重新启动了一个bash子进程,然后执行了bash脚本,所以当然不会改变当前的环境变量了。
© Stam He for Stam He的博客, 2012. |
Permalink |
One comment |
Add to
del.icio.us
Post tags:
Feed enhanced by Better Feed from Ozh