一、概念对比
sh 方式
使用$ sh test.sh执行脚本时,当前shell是父进程,生成一个子shell进程,在子shell中执行脚本。脚本执行完毕,退出子shell,回到当前shell。文章源自小柒网-https://www.i7ti.cn/1206.html
./test.sh与sh test.sh等效。文章源自小柒网-https://www.i7ti.cn/1206.html
source方式
使用$ source test.sh方式,在当前上下文中执行脚本,不会生成新的进程。脚本执行完毕,回到当前shell。文章源自小柒网-https://www.i7ti.cn/1206.html
source方式也叫点命令。文章源自小柒网-https://www.i7ti.cn/1206.html
.test.sh与source test.sh等效。文章源自小柒网-https://www.i7ti.cn/1206.html
exec方式
使用exec command方式,会用command进程替换当前shell进程,并且保持PID不变。执行完毕,直接退出,不回到之前的shell环境。文章源自小柒网-https://www.i7ti.cn/1206.html
二、测试验证
编写test.sh测试脚本文章源自小柒网-https://www.i7ti.cn/1206.html
vim test.sh文章源自小柒网-https://www.i7ti.cn/1206.html
#!/bin/bash
if [ 1 -eq 1 ];then
echo $$
sleep 1
fi
# 显示当前进程PID文章源自小柒网-https://www.i7ti.cn/1206.html
[root@Test ~]# echo $$文章源自小柒网-https://www.i7ti.cn/1206.html
7475文章源自小柒网-https://www.i7ti.cn/1206.html
# sh的方式:执行test.sh打印执行进程PID文章源自小柒网-https://www.i7ti.cn/1206.html
[root@Test ~]# sh test.sh文章源自小柒网-https://www.i7ti.cn/1206.html
9413文章源自小柒网-https://www.i7ti.cn/1206.html
# source方式:执行test.sh打印执行进程PID文章源自小柒网-https://www.i7ti.cn/1206.html
[root@Test ~]# source test.sh文章源自小柒网-https://www.i7ti.cn/1206.html
7475文章源自小柒网-https://www.i7ti.cn/1206.html
# exec方式:执行test.sh打印执行进程PID文章源自小柒网-https://www.i7ti.cn/1206.html
[root@Test ~]# chmod +x test.sh文章源自小柒网-https://www.i7ti.cn/1206.html
[root@Test ~]# exec ./test.sh文章源自小柒网-https://www.i7ti.cn/1206.html
7475文章源自小柒网-https://www.i7ti.cn/1206.html
# 如果没有退出,按下ctrl+C文章源自小柒网-https://www.i7ti.cn/1206.html
Connection closed by foreign host.文章源自小柒网-https://www.i7ti.cn/1206.html
Disconnected from remote host(172.168.1.150) at 16:22:18.文章源自小柒网-https://www.i7ti.cn/1206.html
Type `help' to learn how to use Xshell prompt.文章源自小柒网-https://www.i7ti.cn/1206.html
测试结论:文章源自小柒网-https://www.i7ti.cn/1206.html
sh方式:父进程是7475,执行test.sh时的子进程是9413。执行完毕后回到父进程shell。文章源自小柒网-https://www.i7ti.cn/1206.html
source方式:父进程和子进程都是7475(执行时没有新的进程),执行完毕会回到父进程shell。文章源自小柒网-https://www.i7ti.cn/1206.html
exec方式:进程PID没有改变都是7475,执行完毕(ctrl+C强制关闭)时直接退出了shell。脚本执行时替换了父进程的shell,执行完毕后直接退出,没有回到之前的shell。文章源自小柒网-https://www.i7ti.cn/1206.html
实际经验说明文章源自小柒网-https://www.i7ti.cn/1206.html
工作中我曾用脚本中执行下面命令,用来关闭tomcat。但catalina.sh报错后,我的脚本直接中断。后面其他代码都不再执行了。文章源自小柒网-https://www.i7ti.cn/1206.html
exec /usr/local/tomcat/bin/catalina.sh stop文章源自小柒网-https://www.i7ti.cn/1206.html
所以修改为sh执行,无论exec是否报错,catalina.sh执行完毕后会回到原来shell。我脚本中的后续代码是继续执行的。这个非常有用。文章源自小柒网-https://www.i7ti.cn/1206.html
sh /usr/local/tomcat/bin/catalina.sh stop文章源自小柒网-https://www.i7ti.cn/1206.html
三、使用sh和source方式对上下文的影响
在sh和source方式下,脚本执行完毕,都会回到之前的shell中。但是两种方式对上下文的影响不同。文章源自小柒网-https://www.i7ti.cn/1206.html
此例中,test1.sh脚本执行如下操作:文章源自小柒网-https://www.i7ti.cn/1206.html
1、进入/tmp目录文章源自小柒网-https://www.i7ti.cn/1206.html
2、打印当前工作目录文章源自小柒网-https://www.i7ti.cn/1206.html
3、打印Hello World文章源自小柒网-https://www.i7ti.cn/1206.html
test1.sh脚本文章源自小柒网-https://www.i7ti.cn/1206.html
[root@Test ~]# vim test1.sh文章源自小柒网-https://www.i7ti.cn/1206.html
#!/bin/bash
cd /tmp
pwd
echo "Hello World"
# sh方式执行完毕后,还在当前目录文章源自小柒网-https://www.i7ti.cn/1206.html
[root@Test ~]# sh test1.sh文章源自小柒网-https://www.i7ti.cn/1206.html
/tmp文章源自小柒网-https://www.i7ti.cn/1206.html
Hello World文章源自小柒网-https://www.i7ti.cn/1206.html
# source执行完毕后,当前目录变成了/tmp文章源自小柒网-https://www.i7ti.cn/1206.html
[root@Test ~]# source test1.sh文章源自小柒网-https://www.i7ti.cn/1206.html
/tmp文章源自小柒网-https://www.i7ti.cn/1206.html
Hello World文章源自小柒网-https://www.i7ti.cn/1206.html
[root@Test tmp]#文章源自小柒网-https://www.i7ti.cn/1206.html
结果分析:文章源自小柒网-https://www.i7ti.cn/1206.html
执行结果输出一致,不同在于source执行完毕后目录变成了/tmp文章源自小柒网-https://www.i7ti.cn/1206.html
- 微信号
- 微信扫一扫加我!
-
- 微信公众号
- 微信扫一扫关注我!
-
评论