一. 工具
1. Screen工具
会话介绍:用户打开一个终端窗口,与计算机这种临时的交互,称为一次会话。
会话特点:会话和当前会话启动的进程是相绑定的,当会话关闭,不管进程有没有结束都会随着会话的关闭而结束。
场景 :当用户希望持续ping某个地址时,不希望随着会话的关闭,而让ping 停掉。
作用:新建会话,共享会话。
安装:
Centos7来自于base源,Centos8来自于epel源
yum install screen #centos7安装
centos8安装
dnf -y install epel-release
dnf -y install screen
Screen命令长减用法:
-
创建screen会话
screen -S [SESSION] -
加入screen会话
screen -x [SESSION] -
退出并关闭screen会话
exit -
剥离当前screen会话
ctrl+a,d -
显示所有以打开的screen会话
screen -ls -
恢复某session会话
screen -r [SESSION]
(1)持续ping
(2)可以看到后台ping命令是存在的。
(3)当ping命令的窗口会话关闭时再次看ping进程是否存在。
(4)使用screen命令新建会话之后,再次ping,然后关闭窗口,可以发现后台ping没有随着窗口会话的关闭而结束。
(5)可以使用命令查看后台会话ID, -x加入指定会话ID,或者使用-r恢复会话即可。[08:54:09 maple@centos8-3-node1 ~]$screen -ls There is a screen on: 1954.pts-1.centos8-3-node1 (Detached) 1 Socket in /run/screen/S-maple. [08:54:42 maple@centos8-3-node1 ~]$screen -x 1
(6)关闭会话,可以加入会话之后,exit退出。
详细用法,请自行百度。
2.tmux 工具
增强版的screen,允许在单个窗口中,同时访问多个会话。支持垂直和水平拆分。
安装:
yum install -y tmux
启动与退出
[21:03:43 root@centos8-3-node1 ~]#tmux
[21:03:45 root@centos8-3-node1 ~]#exit
logout
tmux ls #查看所有会话
tmux attach -t 0 #重新连接某个已存在的会话
tmux detach #将当前会话与窗口剥离
ctrl+b," #上下窗分隔,ctrl+b之后,快捷键才会生效,再按"
ctrl+b,% #左右窗分隔。
ctrl+b,上下左右, #光标移动到指定窗口
详细用法,请自行百度.
二. 进阶用法
1.历史命令的快捷用法
(1) !* 取上一条命令的所有参数
[09:45:11 root@centos8-3-node1 ~]#ls anaconda-ks.cfg initial-setup-ks.cfg
anaconda-ks.cfg initial-setup-ks.cfg
[09:45:16 root@centos8-3-node1 ~]#cp !* config/
cp anaconda-ks.cfg initial-setup-ks.cfg config/
[09:45:34 root@centos8-3-node1 ~]#ls config/
anaconda-ks.cfg initial-setup-ks.cfg
[09:45:38 root@centos8-3-node1 ~]#
(2) !$ 上一条命令的最后一个参数或者快捷键 esc+.
[09:49:22 root@centos8-3-node1 ~]#ls initial-setup-ks.cfg
initial-setup-ks.cfg
[09:49:32 root@centos8-3-node1 ~]#rm -f config/!$
rm -f config/initial-setup-ks.cfg
[09:49:43 root@centos8-3-node1 ~]#ls config/
anaconda-ks.cfg
[09:49:47 root@centos8-3-node1 ~]#
(3)!command 输入!命令前几个字母,会自动执行历史命令最近且最匹配的指令。
[09:51:41 root@centos8-3-node1 ~]#ls /boot/
config-4.18.0-240.el8.x86_64 initramfs-0-rescue-9182d71a3a4249be8d38721320fdbc84.img lost+found vmlinuz-4.18.0-240.el8.x86_64
efi initramfs-4.18.0-240.el8.x86_64.img System.map-4.18.0-240.el8.x86_64
grub2 loader vmlinuz-0-rescue-9182d71a3a4249be8d38721320fdbc84
[09:51:51 root@centos8-3-node1 ~]#ls /
bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[09:51:54 root@centos8-3-node1 ~]#!ls
ls /
bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[09:51:57 root@centos8-3-node1 ~]#
(4) 执行命令历史的倒数第几条命令
262 ls
263 echo {a..z}
264 echo {a..z..2}
265 echo {1..100}
266 echo {1..100..2}
267 history
268 history -5
269 history
[10:18:11 root@centos8-3-node1 test]#!-7
echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
[10:18:32 root@centos8-3-node1 test]#
2. 命令管道用法有时比shell脚本更简洁
echo 命令计算1到100的和
[09:51:57 root@centos8-3-node1 ~]#echo {1..100} | tr ' ' '+'|bc
5050
echo 命令计算1到100之间奇数的和
[09:56:01 root@centos8-3-node1 ~]#echo {1..100..2} | tr ' ' '+'|bc
2500
如果用shell脚本还需定义变量,写个for循环
seq命令也可以
[16:34:53 root@centos8-3-node1 data]#seq -s+ 100|bc
5050
3. {} 的扩展使用
(1){}可以实现打印重复字符的简化形式
{x1,x2,x3}
{x1..x2}
[10:10:34 root@centos8-3-node1 test]#touch file{1,3,5} file{10..15}
[10:11:00 root@centos8-3-node1 test]#ls
file1 file10 file11 file12 file13 file14 file15 file3 file5
[10:11:01 root@centos8-3-node1 test]#echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
[10:11:56 root@centos8-3-node1 test]#echo {a..z..2}
a c e g i k m o q s u w y
[10:12:10 root@centos8-3-node1 test]#echo {1..100}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
[10:12:16 root@centos8-3-node1 test]#echo {1..100..2}
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
(2){}可以合并多个程序
{}可以合并多个程序,{后面必须跟个空格,最好一条命令跟;结束,不然报错。
()也可以实现,中间不需要空格,最后也不需要;结束。
[root@centos8-3-node1 data]#{ ls;pwd;} >b.txt
[root@centos8-3-node1 data]#cat b.txt
a
a.txt
b
[root@centos8-3-node1 data]#(ls;pwd)>b.txt
[root@centos8-3-node1 data]#cat b.txt
a
a.txt
b
4.软连接在生产的应用
app-v1.0
app-v2.0
ln -s app-v1.0 app #第一版
rm -rf app
ln -s app-v2.0 app #升级第二版
不定时持续更新中····