shell,exec,source执行脚本的区别

简书 · · 1580 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

 

UNIX/Linux中,经常需要写脚本执行自动化工作。执行脚本有多种方式,可以通过shsource或者exec。这些方式有一些区别,需要根据需求选择合适的方式。

sh方式

使用$ sh script.sh执行脚本时,当前shell是父进程,生成一个子shell进程,在子shell中执行脚本。脚本执行完毕,退出子shell,回到当前shell。
$ ./script.sh$ sh script.sh等效。

例子

使用loop.sh脚本反复打印当前进程号。

 

$ vi loop.sh
#!/bin/sh
while [ 1 = 1 ]; do 
    echo $$
    sleep 1
done

查看父shell的进程号,是2454

 

$ echo $$
2454

loop.sh中,打印子shell进程号,是2700

 

$ sh loop.sh
2700
2700

使用htop监控进程树。父进程2454生成了子进程2700,在子进程中执行脚本。

 

source方式

使用$ source script.sh方式,在当前上下文中执行脚本,不会生成新的进程。脚本执行完毕,回到当前shell。
source方式也叫点命令,$ . script.sh$ source script.sh等效。注意在点命令中,.script.sh之间有一个空格。

 

$ source loop.sh
2454
2454

 

$ . ~/loop.sh 
2454
2454

使用htop监控进程树。在进程2454中直接执行脚本。

man source解释source在当前shell环境中执行脚本。

 

source filename [arguments]
              Read and execute commands from filename in the current shell environment and
              return the exit status of the last command executed from filename. 

exec方式

使用exec command方式,会用command进程替换当前shell进程,并且保持PID不变。执行完毕,直接退出,不回到之前的shell环境。

查看当前shell的进程号为2364

pic1.png

 

使用exec打印进程号,进程号不变,还是2364。打印完毕,直接退出shell。

 

riversec@rcs:~$ exec echo $$
2364
Connection to 192.168.2.123 closed.

man exec解释exec替代当前shell,执行脚本。

 

exec [-cl] [-a name] [command [arguments]]
              If  command is specified, it replaces the shell.  No new process is created.
              The arguments become the arguments to command.  

使用sh和source方式对上下文的影响

shsource方式下,脚本执行完毕,都会回到之前的shell中。但是两种方式对上下文的影响不同呢。

此例中,jump.sh脚本执行如下操作:1)跳到/,2)打印当前工作目录,3)打印Hello

 

$ vi jump.sh
#!/bin/sh
cd /
pwd
echo Hello

通过sh执行脚本时,修改的上下文不会影响当前shell。jump.sh退出以后,工作目录保持不变。

 

$ pwd
/home/riversec
$ ./jump.sh 
/
Hello
$ pwd
/home/riversec

通过source执行脚本时,修改的上下文会影响当前shell。jump.sh退出以后,当前工作目录变成了/

 

$ pwd
/home/riversec
$ source jump.sh 
/
Hello
$ pwd
/



作者:柔软的胖
链接:https://www.jianshu.com/p/dd7956aec097
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

本文来自:简书

感谢作者:简书

查看原文:shell,exec,source执行脚本的区别

1580 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传