source exec 区别(shell 13问中摘出)

exec也是让script在同一个进程上执行,但是原有进程则被结束了

 

脚本内容:

root@james-desktop:/opt/qt-everywhere/marvell/APP/applications/factory-roast# cat 1.sh
#!/bin/sh
A=B
echo "PID for 1.sh :$$"
export A
echo "1.sh \$A is $A"
case $1 in
        exec)
                echo "using exec..."
                exec ./2.sh;;
        source)
                echo "using source..."
                source ./2.sh;;
        *)
                echo "using fork..."
                ./2.sh;;
esac

echo "PID for 1.sh after run 2.sh: $$"
echo "1.sh \$A is $A"

root@james-desktop:/opt/qt-everywhere/marvell/APP/applications/factory-roast# cat 2.sh
#!/bin/sh
echo "PID for 2.sh :$$"

echo "2.sh get \$A=$A from 1.sh"
A=C
export A
echo "2.sh \$A is $A"

运行结果:

root@james-desktop:/opt/qt-everywhere/marvell/APP/applications/factory-roast# ./1.sh
PID for 1.sh :8152
1.sh $A is B
using fork...
PID for 2.sh :8153
2.sh get $A=B from 1.sh
2.sh $A is C
PID for 1.sh after run 2.sh: 8152
1.sh $A is B

 

root@james-desktop:/opt/qt-everywhere/marvell/APP/applications/factory-roast# ./1.sh source
PID for 1.sh :8158
1.sh $A is B
using source...
PID for 2.sh :8158
2.sh get $A=B from 1.sh
2.sh $A is C
PID for 1.sh after run 2.sh: 8158
1.sh $A is C

 

root@james-desktop:/opt/qt-everywhere/marvell/APP/applications/factory-roast# ./1.sh exec
PID for 1.sh :8159
1.sh $A is B
using exec...
PID for 2.sh :8159
2.sh get $A=B from 1.sh
2.sh $A is C

posted on 2011-07-01 20:16  katago  阅读(267)  评论(0编辑  收藏  举报