认识shell
查看已安装的shell
┌──(kali㉿DESKTOP-FQ0BCLS)-[/mnt/e/Edgedownload]
└─$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash
/usr/bin/sh
/usr/bin/pwsh
/opt/microsoft/powershell/7/pwsh
/bin/zsh
/usr/bin/zsh
/usr/bin/tmux
/usr/bin/screen
查看系统默认shell
┌──(kali㉿DESKTOP-FQ0BCLS)-[/mnt/e/Edgedownload]
└─$ which sh
/usr/bin/sh
┌──(kali㉿DESKTOP-FQ0BCLS)-[/mnt/e/Edgedownload]
└─$ ls -l /usr/bin/sh
lrwxrwxrwx 1 root root 4 Sep 1 22:11 /usr/bin/sh -> dash
显示当前的shell名称
┌──(kali㉿DESKTOP-FQ0BCLS)-[/mnt/e/Edgedownload]
└─$ echo $0
bash
输入shell的名称即可切换bash
exit退出bash
shell的父子关系
PID 当前进程的ID
PPID 当前进程的父进程的ID
ps --forest 查看继承关系
ps -f 查看进程的常用详细信息
ps -l 详细信息
实时查看进程 top
结束进程 kill PID
例
┌──(kali㉿DESKTOP-FQ0BCLS)-[/mnt/e/Edgedownload]
└─$ ps -f
UID PID PPID C STIME TTY TIME CMD
kali 10114 10113 0 Dec26 pts/0 00:00:00 bash
kali 10341 10114 0 14:39 pts/0 00:00:00 ps -f
┌──(kali㉿DESKTOP-FQ0BCLS)-[/mnt/e/Edgedownload]
└─$ bash
┌──(kali㉿DESKTOP-FQ0BCLS)-[/mnt/e/Edgedownload]
└─$ ps -f
UID PID PPID C STIME TTY TIME CMD
kali 10114 10113 0 Dec26 pts/0 00:00:00 bash
kali 10342 10114 1 14:39 pts/0 00:00:00 bash
kali 10345 10342 0 14:39 pts/0 00:00:00 ps -f
┌──(kali㉿DESKTOP-FQ0BCLS)-[/mnt/e/Edgedownload]
└─$ ps --forest
PID TTY TIME CMD
10114 pts/0 00:00:00 bash
10342 pts/0 00:00:00 \_ bash
10346 pts/0 00:00:00 \_ ps
┌──(kali㉿DESKTOP-FQ0BCLS)-[/mnt/e/Edgedownload]
└─$ exit
exit
┌──(kali㉿DESKTOP-FQ0BCLS)-[/mnt/e/Edgedownload]
└─$ ps -f
UID PID PPID C STIME TTY TIME CMD
kali 10114 10113 0 Dec26 pts/0 00:00:00 bash
kali 10347 10114 0 14:39 pts/0 00:00:00 ps -f
进程列表
命令列表(命令间用分号分隔)
┌──(kali㉿DESKTOP-FQ0BCLS)-[/mnt/e/Edgedownload]
└─$ pwd;cd /etc;pwd;echo $BASH_SUBSHELL
/mnt/e/Edgedownload
/etc
0
进程列表(将命令列表放入小括号中)
生成一个子shell执行命令。可以嵌套括号生成子子shell
如何查看是否生成子shell?echo $BASH_SUBSHELL
,非0为子shell。
┌──(kali㉿DESKTOP-FQ0BCLS)-[/mnt/e/Edgedownload]
└─$ (pwd;cd /etc;pwd;echo $BASH_SUBSHELL)
/mnt/e/Edgedownload
/etc
1
灵活运用子shell
后台模式(在命令行末尾加上&,将命令置于后台)
┌──(kali㉿DESKTOP-FQ0BCLS)-[/mnt/e/Edgedownload]
└─$ sleep 1000&
[1] 10350
┌──(kali㉿DESKTOP-FQ0BCLS)-[/mnt/e/Edgedownload]
└─$ jobs
[1]+ Running sleep 1000 &
[1] 后台作业号
10350 PID
jobs 查看所有后台进程
将进程放入后台后,终端不再与子shell的IO绑定
协程(coproc)
┌──(kali㉿DESKTOP-FQ0BCLS)-[/mnt/e/Edgedownload]
└─$ coproc sleep 10;ps --forest
[1] 10354
PID TTY TIME CMD
10114 pts/0 00:00:00 bash
10354 pts/0 00:00:00 \_ sleep
10355 pts/0 00:00:00 \_ ps
coproc与嵌套子进程
┌──(kali㉿DESKTOP-FQ0BCLS)-[/mnt/e/Edgedownload]
└─$ coproc (sleep 10;sleep 2)
bash: warning: execute_coproc: coproc [10365:COPROC] still exists
[4] 10367
[2] Done coproc COPROC ( sleep 10 )
[3] Done coproc COPROC ( sleep 10 )
┌──(kali㉿DESKTOP-FQ0BCLS)-[/mnt/e/Edgedownload]
└─$ ps --forest
PID TTY TIME CMD
10114 pts/0 00:00:00 bash
10367 pts/0 00:00:00 \_ bash
10368 pts/0 00:00:00 | \_ sleep
10369 pts/0 00:00:00 \_ ps
协程命名
# 默认名称
┌──(kali㉿DESKTOP-FQ0BCLS)-[/mnt/e/Edgedownload]
└─$ coproc sleep 10;jobs
[1] 10375
[1]+ Running coproc COPROC sleep 10 &
# 自定义名称 注意格式{空格xxx;空格}
┌──(kali㉿DESKTOP-FQ0BCLS)-[/mnt/e/Edgedownload]
└─$ coproc MY_COPROC { sleep 10; }
[1] 10372
┌──(kali㉿DESKTOP-FQ0BCLS)-[/mnt/e/Edgedownload]
└─$ jobs
[1]+ Done coproc MY_COPROC { sleep 10; }
外部命令和内建命令
使用type -a 命令名
查看是外部命令还是内建命令
使用which 查看当前使用命令的安装位置
外部命令
位于 .../bin 文件夹中
执行时衍生(创建子进程)
内建命令
和shell集成的命令,高效
例
┌──(kali㉿DESKTOP-FQ0BCLS)-[/mnt/e/Edgedownload]
└─$ type -a echo
echo is a shell builtin <--echo是内建命令
echo is /usr/bin/echo
echo is /bin/echo
[1]+ Done coproc COPROC sleep 10
┌──(kali㉿DESKTOP-FQ0BCLS)-[/mnt/e/Edgedownload]
└─$ which echo
/usr/bin/echo <--当前使用的echo是外部命令
┌──(kali㉿DESKTOP-FQ0BCLS)-[/mnt/e/Edgedownload]
└─$ type -a ps
ps is /usr/bin/ps <--ps是外部命令
ps is /bin/ps
两个常用的内建命令
history 查看历史使用的命令行
alias 设置命令行别名