- 1 Shell种类
- 2 父子shell
- 2.1 概念
- 2.2 使用子shell
- 2.2.1 进程列表
- 2.2.2 后台
- 2.2.3 协程
- 3 内建和非内建命令
- 3.1 外部命令
- 3.2 内建命令
- 3.2.1 history
- 3.2.2 alias
查看自己的shell类型:
cat /etc/passwd
常见shell的种类:
- bash shell
- tcsh【源自csh】
- dash【ash shell的Debian版】
- csh【C shell的软链】
- bash【默认使用】
查看shell信息:
ls -lF /bin/tcsh
查看系统默认的shell:
ls -l /bin/sh
结果:
lrwxrwxrwx. 1 root root 4 12月 2 2020 /bin/sh -> bash2 父子shell
在shell脚本中,经常使用子shell进行多进程处理,但是它仍然有许多缺点。
缺点:
- 脚本中:
- 明显拖慢处理速度;
- 交互式的CLI shell中:
- 终端控制着子shell的I/O,子shell并非真正的多进程处理;
用处:
- 后台模式;
- 进程列表;
- 协程;
- 管道;
使用bash创建子shell,然后ps -f来查看是否是子shell。
例如:
[root@hdp-yl-1 ~]# bash [root@hdp-yl-1 ~]# ps -f UID PID PPID C STIME TTY TIME CMD root 3014 2967 0 16:47 pts/0 00:00:00 -bash root 3172 3014 1 16:47 pts/0 00:00:00 bash root 3322 3172 0 16:47 pts/0 00:00:00 ps -f
如果是创建多个子shell,
[root@hdp-yl-1 ~]# bash [root@hdp-yl-1 ~]# bash [root@hdp-yl-1 ~]# bash [root@hdp-yl-1 ~]# bash [root@hdp-yl-1 ~]# ps -f UID PID PPID C STIME TTY TIME CMD root 7279 7233 0 16:49 pts/0 00:00:00 -bash root 15113 7279 0 16:51 pts/0 00:00:00 bash root 15258 15113 0 16:51 pts/0 00:00:00 bash root 15368 15258 0 16:51 pts/0 00:00:00 bash root 15506 15368 0 16:51 pts/0 00:00:00 bash root 15705 15506 0 16:51 pts/0 00:00:00 ps -f [root@hdp-yl-1 ~]# ps --forest PID TTY TIME CMD 7279 pts/0 00:00:00 bash 15113 pts/0 00:00:00 _ bash 15258 pts/0 00:00:00 _ bash 15368 pts/0 00:00:00 _ bash 15506 pts/0 00:00:00 _ bash 16235 pts/0 00:00:00 _ ps
可以通过echo $BASH_SUBSHELL来判断是否生成了子shell,如果该命令返回0,就表明没有子shell,数字几,就表示有几个子shell。
2.2 使用子shell 2.2.1 进程列表可以通过使用进程列表来启动子shell执行执行指令,
不使用子shell,
[root@hdp-yl-1 usr]# ls;date;ps -f;echo $BASH_SUBSHELL bin etc games include lib lib64 libexec local sbin share src tmp 2021年 09月 29日 星期三 16:59:32 CST UID PID PPID C STIME TTY TIME CMD root 1031 988 0 16:57 pts/0 00:00:00 -bash root 8550 1031 0 16:59 pts/0 00:00:00 ps -f 0
使用进程列表【一种放在括号中的命令分组,
[root@hdp-yl-1 usr]# (ls;date;ps -f;echo $BASH_SUBSHELL) bin etc games include lib lib64 libexec local sbin share src tmp 2021年 09月 29日 星期三 16:59:09 CST UID PID PPID C STIME TTY TIME CMD root 1031 988 0 16:57 pts/0 00:00:00 -bash root 7274 1031 0 16:59 pts/0 00:00:00 -bash root 7277 7274 0 16:59 pts/0 00:00:00 ps -f 12.2.2 后台
在CLI中运用子shell的创造性方法之一就是将进程列表置入后台模式。你既可以在子shell中进行繁重的处理工作,同时也不会让子shell的I/O受制于终端。
例子:
(sleep 2;echo $BASH_SUBSHELL;sleep 2)&
可以通过jobs,显示出当前运行在后台模式中的所有用户的进程(作业)。
例如:
[root@hdp-yl-1 usr]# jobs -l [1]+ 7793 运行中 sleep 200 &2.2.3 协程
协程可以同时做两件事。它在后台生成一个子shell,并在这个子shell中执行命令。
需要使用关键字coproc命令.
例如:
coproc My_Job { sleep 10; }
这里使用扩展语法,协程的名字被设置成My_Job。【注意:每个花括号前后都必须有空格】
也可以在携程中嵌套进程列表:
[root@hdp-yl-1 usr]# coproc (sleep 15; sleep 2) [1] 23935 [root@hdp-yl-1 usr]# ps --forest PID TTY TIME CMD 1031 pts/0 00:00:00 bash 23935 pts/0 00:00:00 _ bash 23936 pts/0 00:00:00 | _ sleep 23939 pts/0 00:00:00 _ ps3 内建和非内建命令
判断是否有是内建命令,使用type或which。例如:
[root@hdp-jiangxue-4 ~]# which ps /usr/bin/ps [root@hdp-jiangxue-4 ~]# type -a ps ps 是 /usr/bin/ps3.1 外部命令
外部命令【文件系统命令】:是存在于bash shell之外的程序,通常位于/bin、/usr/bin、/sbin或/usr/sbin中。例如ps。
如果使用外部命令,需要花费时间和精力来设置新子进程的环境。
3.2 内建命令内建命令:不需要使用子进程来执行。它们已经和shell编译成了一
体,作为shell工具的组成部分存在。不需要借助外部程序文件来运行。例如cd。
有些命令有多种实现,例如echo和pwd既有内建命令也有外部命令。
[root@hdp-jiangxue-4 ~]# type -a echo echo 是 shell 内嵌 echo 是 /usr/bin/echo3.2.1 history
作用:显示历史指令【命令历史记录被保存在隐藏文件.bash_history中(位于用户的主目录中),bash命令的历史记录是先存放在内存中,当shell退出时才被写入到历史文件中。】
- 使用上条历史记录
当输入!!时,bash首先会显示出从shell的历史记录中唤回的命令。然后执行该命令。例如:
[root@hdp-k-4 ~]# ps -f UID PID PPID C STIME TTY TIME CMD root 64688 64686 0 09:38 pts/0 00:00:00 -bash root 65002 64688 0 09:56 pts/0 00:00:00 ps -f [root@hdp-k-4 ~]# !! ps -f UID PID PPID C STIME TTY TIME CMD root 64688 64686 0 09:38 pts/0 00:00:00 -bash root 65003 64688 0 09:56 pts/0 00:00:00 ps -f
- 强制更新
可以在退出shell会话之前强制将命令历史记录写入.bash_history文件,使用history -a
- 重新载入历史命令记录
.bash_history文件只有在打开首个终端会话时才会被读取。要想强制重新读取.bash_history文件,更新终端会话的历史记录,可以使用history -n命令。
- 唤回历史命令
唤回历史列表中任意一条命令。只需输入惊叹号和命令在历史列表中的编号。例如:
# history # !10003.2.2 alias
alias:命令别名允许你为常用的命令(及其参数)创建另一个名称,从而将输入量减少到最低。
- 查看linux内置的别名
Linux发行版很有可能已经设置好了一些常用命令的别名,使用alias -p查看
[root@hdp-k-4 ~]# alias -p alias cp='cp -i' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' alias mv='mv -i' alias rm='rm -i' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
- 创建属于自己的别名
一个别名仅在它所被定义的shell进程中才有效。
[root@hdp-k-4 ~]# alias li='ls -li' [root@hdp-k-4 ~]# li 总用量 8 2144828 drwxr-xr-x 2 root root 52 9月 23 16:34 airflow 134296642 -rw-------. 1 root root 1565 12月 2 2020 anaconda-ks.cfg 139038581 -rw-r--r-- 1 root root 395 6月 15 11:24 ifcfg-ens192 [root@hdp-k-4 ~]# bash [root@hdp-k-4 ~]# li bash: li: 未找到命令 [root@hdp-k-4 ~]# exit exit



