linux shell 编程 9 脚本中调用脚本
2015-11-11 00:00
561 查看
摘要: 在shell脚本中调用另一个脚本的三种不同方法(fork, exec, source)
在shell脚本中调用另一个脚本的三种不同方法(fork, exec, source)
1、fork 调用脚本
fork ( /directory/script.sh)
fork是最普通的, 就是直接在脚本里面用/directory/script.sh来调用script.sh这个脚本.
运行的时候开一个sub-shell执行调用的脚本,sub-shell执行的时候, parent-shell还在。
sub-shell执行完毕后返回parent-shell. sub-shell从parent-shell继承环境变量.但是sub-shell中的环境变量不会带回parent-shell
2、exec 调用脚本
exec (exec /directory/script.sh)
exec与fork不同,不需要新开一个sub-shell来执行被调用的脚本. 被调用的脚本与父脚本在同一个shell内执行。但是使用exec调用一个新脚本以后, 父脚本中exec行之后的内容就不会再执行了。这是exec和source的区别
3、source 调用脚本
source (source /directory/script.sh)
与fork的区别是不新开一个sub-shell来执行被调用的脚本,而是在同一个shell中执行. 所以被调用的脚本中声明的变量和环境变量, 都可以在主脚本中得到和使用.
可以通过下面这两个脚本来体会三种调用方式的不同:
1、 脚本1 t1.sh
#!/bin/bash
A=B
echo "PID for 1.sh before exec/source/fork:$$"
# Environment variable
export A
# value of A is B
echo "1.sh: \$A is $A"
case $1 in
exec)
echo "using exec…"
exec ./t2.sh ;;
source)
echo "using source…"
source ./t2.sh ;;
*)
echo "using fork by default…"
./t2.sh ;;
esac
echo "PID for 1.sh after exec/source/fork:$$"
echo "1.sh: \$A is $A"
2、 脚本2 t2.sh
#!/bin/bash
echo "PID for 2.sh: $$"
echo "2.sh get \$A=$A from 1.sh"
A=C
#Environment variable
export A
echo "2.sh: \$A is $A"
执行结果:
1、默认方式 fork, 在执行调用的 sh时 会新开一个 sub_shell而sub_shell 执行完parent_shell 会继续执行
[root@localhost src]# sh t1.sh
PID for 1.sh before exec/source/fork:4221
1.sh: $A is B
using fork by default…
PID for 2.sh: 4222
2.sh get $A=B from 1.sh
2.sh: $A is C
PID for 1.sh after exec/source/fork:4221
1.sh: $A is B
2、exec 执行结果 sub_shell 执行完后parent_shell J就不执行了
[root@localhost src]# sh t1.sh exec
PID for 1.sh before exec/source/fork:4263
1.sh: $A is B
using exec…
PID for 2.sh: 4263
2.sh get $A=B from 1.sh
2.sh: $A is C
3、source 不会新开 sub_shell 来执行调用的脚本而是在同一个shell 中执行
[root@localhost src]# sh t1.sh source
PID for 1.sh before exec/source/fork:4290
1.sh: $A is B
using source…
PID for 2.sh: 42
7fe0
90
2.sh get $A=B from 1.sh
2.sh: $A is C
PID for 1.sh after exec/source/fork:4290
1.sh: $A is C
在shell脚本中调用另一个脚本的三种不同方法(fork, exec, source)
1、fork 调用脚本
fork ( /directory/script.sh)
fork是最普通的, 就是直接在脚本里面用/directory/script.sh来调用script.sh这个脚本.
运行的时候开一个sub-shell执行调用的脚本,sub-shell执行的时候, parent-shell还在。
sub-shell执行完毕后返回parent-shell. sub-shell从parent-shell继承环境变量.但是sub-shell中的环境变量不会带回parent-shell
2、exec 调用脚本
exec (exec /directory/script.sh)
exec与fork不同,不需要新开一个sub-shell来执行被调用的脚本. 被调用的脚本与父脚本在同一个shell内执行。但是使用exec调用一个新脚本以后, 父脚本中exec行之后的内容就不会再执行了。这是exec和source的区别
3、source 调用脚本
source (source /directory/script.sh)
与fork的区别是不新开一个sub-shell来执行被调用的脚本,而是在同一个shell中执行. 所以被调用的脚本中声明的变量和环境变量, 都可以在主脚本中得到和使用.
可以通过下面这两个脚本来体会三种调用方式的不同:
1、 脚本1 t1.sh
#!/bin/bash
A=B
echo "PID for 1.sh before exec/source/fork:$$"
# Environment variable
export A
# value of A is B
echo "1.sh: \$A is $A"
case $1 in
exec)
echo "using exec…"
exec ./t2.sh ;;
source)
echo "using source…"
source ./t2.sh ;;
*)
echo "using fork by default…"
./t2.sh ;;
esac
echo "PID for 1.sh after exec/source/fork:$$"
echo "1.sh: \$A is $A"
2、 脚本2 t2.sh
#!/bin/bash
echo "PID for 2.sh: $$"
echo "2.sh get \$A=$A from 1.sh"
A=C
#Environment variable
export A
echo "2.sh: \$A is $A"
执行结果:
1、默认方式 fork, 在执行调用的 sh时 会新开一个 sub_shell而sub_shell 执行完parent_shell 会继续执行
[root@localhost src]# sh t1.sh
PID for 1.sh before exec/source/fork:4221
1.sh: $A is B
using fork by default…
PID for 2.sh: 4222
2.sh get $A=B from 1.sh
2.sh: $A is C
PID for 1.sh after exec/source/fork:4221
1.sh: $A is B
2、exec 执行结果 sub_shell 执行完后parent_shell J就不执行了
[root@localhost src]# sh t1.sh exec
PID for 1.sh before exec/source/fork:4263
1.sh: $A is B
using exec…
PID for 2.sh: 4263
2.sh get $A=B from 1.sh
2.sh: $A is C
3、source 不会新开 sub_shell 来执行调用的脚本而是在同一个shell 中执行
[root@localhost src]# sh t1.sh source
PID for 1.sh before exec/source/fork:4290
1.sh: $A is B
using source…
PID for 2.sh: 42
7fe0
90
2.sh get $A=B from 1.sh
2.sh: $A is C
PID for 1.sh after exec/source/fork:4290
1.sh: $A is C
相关文章推荐
- linux shell变量
- shell date处理
- 使用powershell统计office365中exchange用户登录情况
- hdfs shell命令
- Shell重定向文件描述符
- shell批处理中利用vi设置文件的fileformat
- Linux shell 数组使用
- 鸟哥的Linux私房菜读书笔记-第11章-认识与学习bash
- HBase基本概念和hbase shell常用命令用法
- android shell
- Shell脚本教程
- linux shell 正则表达式(BREs,EREs,PREs)差异比较
- mysql触发器内执行shell脚本,shell脚本用curl访问php网页
- awk中使用的shell命令-浅记
- awk调用shell命令
- Shell脚本之awk详解
- 用awk编写Shell脚本
- shell下添加计划性任务
- Awk 命令学习总结、AWk命令系列学习(linux shell)
- shell编程之【告警系统】