shell,exec,source执行脚本

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
/
发布了504 篇原创文章 · 获赞 610 · 访问量 114万+

猜你喜欢

转载自blog.csdn.net/asdfsadfasdfsa/article/details/104097619
今日推荐