使用“.”,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 效果相同

相关推荐
chlk1231 天前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑1 天前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件1 天前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
深紫色的三北六号2 天前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash2 天前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
哈基咪怎么可能是AI2 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行3 天前
Linux和window共享文件夹
linux
木心月转码ing3 天前
WSL+Cpp开发环境配置
linux
崔小汤呀4 天前
最全的docker安装笔记,包含CentOS和Ubuntu
linux·后端
何中应5 天前
vi编辑器使用
linux·后端·操作系统