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

相关推荐
棒棒的唐42 分钟前
armbian平台ubuntu环境下telnet安装及启动,给pantherX2增加一个应急通道
linux·运维·armbian·telnetd
bug攻城狮43 分钟前
CentOS 7 设置静态 IP 地址
linux·tcp/ip·centos
纳切威1 小时前
CentOS 7部署Zabbix5.0
linux·运维·centos·zabbix
sunshine-sm1 小时前
CentOS Steam 9安装 MySQL 8
linux·运维·服务器·数据库·mysql·centos·centos stream
bug攻城狮1 小时前
CentOS 7 快速检查软件包是否已安装的5种方法
linux·运维·centos
DONG9992 小时前
ubuntu 22 安装轻量级桌面Xfce并使用xrdp远程桌面连接
linux·运维·ubuntu
呆萌小新@渊洁3 小时前
linux升级系统,重启出现Minimal BASH-like line editingis supported
linux·服务器
ajassi20003 小时前
开源 C++ QT Widget 开发(十四)多媒体--录音机
linux·c++·qt·开源
zwhSunday3 小时前
Linux驱动开发(2)进一步理解驱动
linux·驱动开发
Miraitowa_cheems4 小时前
LeetCode算法日记 - Day 38: 二叉树的锯齿形层序遍历、二叉树最大宽度
java·linux·运维·算法·leetcode·链表·职场和发展