首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

exec source差别

2012-07-19 
exec source区别exec source区别2011-07-01 15:37exec 和 source 都可以确保在一个(pid)进程内执行,区别就

exec source区别

exec source区别2011-07-01 15:37

exec 和 source 都可以确保在一个(pid)进程内执行,区别就是exec会中断当前的进程

看以下例子即可理解:

1.sh:

#!/bin/bash
A=B
echo "PID for 1.sh before exec/source/fork:$$"
export A
echo "1.sh: \$A is $A"
case $1 in
??????? exec)
??????????????? echo "using exec..."
??????????????? exec ./2.sh ;;
??????? source)
??????????????? echo "using source..."
??????????????? . ./2.sh ;;
??????? *)
??????????????? echo "using fork by default..."
??????????????? ./2.sh ;;
esac
echo "PID for 1.sh after exec/source/fork:$$"
echo "1.sh: \$A is $A"

2.sh:

#!/bin/bash
echo "PID for 2.sh: $$"
echo "2.sh get \$A=$A from 1.sh"
A=C
export A
echo "2.sh: \$A is $A"


运行结果 :

[springhp@localhost ~]$?./1.sh fork
PID for 1.sh before exec/source/fork: 4331
1.sh: $A is B
Using fork by default...
PID for 2.sh : 4332
2.sh get $A = B from 1.sh
2.sh: $A is C
PID for 1.sh after exec/source/fork:4331
1.sh: $A is B
[springhp@localhost ~]$./1.sh exec
PID for 1.sh before exec/source/fork: 4335
1.sh: $A is B
Using exex...
PID for 2.sh : 4335
2.sh get $A = B from 1.sh
2.sh: $A is C
[springhp@localhost ~]$?./1.sh source
PID for 1.sh before exec/source/fork: 4337
1.sh: $A is B
Using source...
PID for 2.sh : 4337
2.sh get $A = B from 1.sh
2.sh: $A is C
PID for 1.sh after exec/source/fork:4337
1.sh: $A is C

热点排行