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

相关推荐
orion5713 小时前
Missing Semester Class1:course overview and introduction of shell
linux
用户1204872216119 小时前
Linux驱动编译与加载
linux·嵌入式
用户805533698031 天前
Input 子系统架构:Core、Handler、Driver 三层是怎么协作的
linux·嵌入式
用户805533698031 天前
RK-Forge外设系列开篇 - 把板子从「能启动」变成「能用」:Ethernet/SPI/MMC 三个纯接线外设
linux·github·嵌入式
七歌杜金房2 天前
我终于又有了自己的 Linux 电脑
linux·debian·mac
tntxia3 天前
linux curl命令详解_curl详解
linux
扛枪的书生3 天前
Linux 网络管理器用法速查
linux
顺风尿一寸3 天前
Java Socket 内核之旅:从 SocketChannel.read() 到 tcp_recvmsg 与 epoll 的完整调用链路
linux
XIAOHEZIcode3 天前
Ubuntu 终端美化全栈指南:Bash 到 Kitty 踩坑实录
linux·ubuntu·命令行
唐青枫3 天前
别再只会用 cron:Linux systemd Timer 定时任务实战详解
linux