使用“.”,sh,source,bash执行脚本的区别

本篇文章演示".",sh,source,bash 执行脚本的区别

使用Centos7.6作为实验环境,实验前需要下载pstree命令,yum -y install psmisc

实验脚本如下:

复制代码
[root@192 ~]# ll -d test.sh
-rw-r--r--. 1 root root 26 Dec 11 06:03 test.sh
[root@192 ~]# cat test.sh
#! /bin/bash

vmstat -n 1

1. 使用 "." 执行脚本shell发生了什么?

使用 "." 执行脚本脚本需要赋予脚本 可执行权限

使用echo $$ 查看当前shell进程号

复制代码
[root@192 ~]# echo $$
11226

[root@192 ~]# chmod +x test.sh
[root@192 ~]# ./test.sh
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0      0 673792   2068 126328    0    0    72     4   98  233  0  2 98  0  0

开启另一个终端使用pstree来观察脚本执行情况

复制代码
[root@192 ~]# pstree -pca 11226
bash,11226
  └─test.sh,44140 ./test.sh
      └─vmstat,44141 -n 1

由此可以看出在执行 ./test.sh 时由本地bash启动一个test.sh 进程,test.sh进程又启动一个 vmstat 进程。

2. 使用sh 执行,shell发生了什么?

使用 sh 执行脚本时,不需要脚本又执行权限,但是一定要有可读权限 ,当前shell进程还是 11226

复制代码
[root@192 ~]# sh test.sh
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0      0 139120      0 652344    0    0    62   109   83  151  0  1 98  0  0

开启另一个终端使用pstree来观察脚本执行情况

复制代码
[root@192 ~]# pstree -pca 11226
bash,11226
  └─sh,39059 test.sh
      └─vmstat,39060 -n 1

由此可以看出 sh test.sh 在执行脚本时,由本地shell bash 开启了一个子shell sh,由子shell sh 在开启 vmstat进程

3. 使用 source 执行,shell发生了什么?

复制代码
[root@192 ~]# source test.sh
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0      0 139212      0 652424    0    0    55    95   83  147  0  1 98  0  0

开启另一个终端使用pstree来观察脚本执行情况

复制代码
[root@192 ~]# pstree -pca 11226
bash,11226
  └─vmstat,44717 -n 1

由此可以看出使用source test.sh 执行脚本时,直接由本地shell进程开启一个子进程执行

4. 使用bash 执行,shell发生了什么?

bash 是当前默认shell,使用 echo $SHELL 查看当前shell

root@192 \~\]# echo $SHELL /bin/bash

复制代码
[root@192 ~]# bash test.sh
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0      0 139124      0 652372    0    0    53    93   82  146  0  1 98  0  0
 0  0      0 138860      0 652372    0    0     0     0   69  126  0  1 99  0  0

开启另一个终端使用pstree来观察脚本执行情况

复制代码
[root@192 ~]# pstree -pca 11226
bash,11226
  └─bash,45904 test.sh
      └─vmstat,45905 -n 1

由此可以看出在执行 bash test.sh 时,先由本地shell 进程,开启一个子bash进程执行test.sh 脚本,在由test.sh进程开启 一个vmstat进程,这个效果和sh test.sh 效果相同

相关推荐
三体世界3 分钟前
Linux --TCP协议实现简单的网络通信(中英翻译)
linux·c语言·开发语言·网络·c++·windows·tcp/ip
Johny_Zhao2 小时前
Burp Suite 企业级深度实战教程
linux·网络·网络安全·信息安全·云计算·shell·burp suite·系统运维·itsm
良辰美景好时光2 小时前
keepalived定制日志bug
linux·运维·bug
s_little_monster3 小时前
【Linux】网络--网络层--IP协议
linux·运维·网络·经验分享·笔记·学习·tcp/ip
forward_huan3 小时前
wsl安装linux
linux·wsl
西阳未落3 小时前
Linux(10)——第二个小程序(自制shell)
linux·运维·服务器
chiou7223 小时前
为 Ubuntu 安装的软件创建桌面图标
linux·ubuntu·lua
binary思维4 小时前
Ubuntu挂起和休眠
linux·运维·ubuntu
初叶 crmeb4 小时前
JAVA单商户易联云小票打印替换模板
java·linux·python
qq_447429414 小时前
数据结构与算法:图论——拓扑排序
linux·c语言·学习·图论