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

在UNIX/Linux中,经常需要写脚本执行自动化工作。执行脚本有多种方式,可以通过sh,source或者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,在子进程中执行脚本。

dd7956aec097

source方式

使用$ source script.sh方式,在当前上下文中执行脚本,不会生成新的进程。脚本执行完毕,回到当前shell。

source方式也叫点命令,$ . script.sh与$ source script.sh等效。注意在点命令中,.与script.sh之间有一个空格。

$ source loop.sh

2454

2454

$ . ~/loop.sh

2454

2454

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

dd7956aec097

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。

dd7956aec097

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方式对上下文的影响

在sh和source方式下,脚本执行完毕,都会回到之前的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

/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值