shell实践
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uHlj4Hu6-1678231802090)(http://book.luffycity.com/linux-book/Shell%E5%9F%BA%E7%A1%80/pic/image-20210301155751435.png)]
父子shell
父shell:我们在登录某个虚拟机控制器终端的时候(连接某一个linux虚拟机)时,默认启动的交互式shell,然后等待命令输入。
ps命令参数,是否有横杠的参数作用是不一样的
-f 显示UID,PPID,C与STIME栏位。
f 用ASCII字符显示树状结构,表达进程间的相互关系。
-e 此参数的效果和指定"A"参数相同。
e 列出进程时,显示每个进程所使用的环境变量。
案例
1.于超老师登录自己的虚拟机
[yuchao@yumac Luffy_linux]$sshpyyu
Last login: Sat Sep 26 21:06:16 2020 from 221.218.215.96
[root@chaogelinux ~]#
2.一条命令,查看进程的父子关系
[root@chaogelinux ~]# ps --forest -ef
# 观察如下信息,可以清晰看出父子关系
root 1830 1 0 9月25 ? 00:00:00 /usr/sbin/sshd -D
root 15105 1830 0 21:07 ? 00:00:00 \_ sshd: root@pts/0
root 15107 15105 0 21:07 pts/0 00:00:00 \_ -bash
root 16074 15107 0 21:11 pts/0 00:00:00 \_ ps --forest -ef
子shell
当在CLI的提示符下,输入/bin/bash指令,或者其他bash指令,会创建一个新的shell程序,这就被称之为子shell(child shell)
子shell同样的拥有CLI提示符,可以输入命令。
使用如下命令,超哥教你如何查看父子的诞生
[root@chaogelinux ~]# ps -f
UID PID PPID C STIME TTY TIME CMD
root 15107 15105 0 21:07 pts/0 00:00:00 -bash
root 16893 15107 0 21:17 pts/0 00:00:00 ps -f
当前父shell 15107
[root@chaogelinux ~]# ps -f
UID PID PPID C STIME TTY TIME CMD
root 15107 15105 0 21:07 pts/0 00:00:00 -bash
root 16966 15107 1 21:18 pts/0 00:00:00 bash
root 17144 16966 0 21:18 pts/0 00:00:00 ps -f
第一次父bash的pid,15107
第二次执行bash,子shell的pid, 16966,ppid是15107,由此看出是子shell
输入bash指令之后,一个子shell就产生了。
- 第一个ps -ef命令是在父shell里执行的
- 第二个ps -ef是在子shell里执行的。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wGTqmrxz-1678231802093)(http://book.luffycity.com/linux-book/Shell%E5%9F%BA%E7%A1%80/pic/image-20200927092127774.png)]
子shell生成时,父进程的部分环境变量被复制到子shell里,这个后面于超老师在给大家说。
多个子shell
1.当前shell关系
root 1830 1 0 9月25 ? 00:00:00 /usr/sbin/sshd -D
root 22206 1830 0 09:23 ? 00:00:00 \_ sshd: root@pts/2
root 22208 22206 0 09:23 pts/2 00:00:00 \_ -bash
root 22757 22208 0 09:24 pts/2 00:00:00 \_ ps -ef --forest
2.执行多个bash,开启多个子shell
连续输入四次bash之后
root 1830 1 0 9月25 ? 00:00:00 /usr/sbin/sshd -D
root 22206 1830 0 09:23 ? 00:00:00 \_ sshd: root@pts/2
root 22208 22206 0 09:23 pts/2 00:00:00 \_ -bash
root 22844 22208 0 09:25 pts/2 00:00:00 \_ bash
root 23017 22844 0 09:25 pts/2 00:00:00 \_ bash
root 23190 23017 1 09:25 pts/2 00:00:00 \_ bash
root 23363 23190 1 09:25 pts/2 00:00:00 \_ bash
root 23537 23363 0 09:25 pts/2 00:00:00 \_ ps -ef --forest
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1Guttwux-1678231802095)(http://book.luffycity.com/linux-book/Shell%E5%9F%BA%E7%A1%80/pic/image-20200927092630728.png)]
退出子shell
exit 可以退出子shell,也可以退出当前的虚拟控制台终端。
只需要在父shell里输入exit就可以退出了。
<