shell

shell是一个命令解释器,提供用户与机器之间的交互,支持特定的语法(逻辑判断、循环等); 每个用户都可以有自己特定的shell; centos7默认shell为bash,其他shell还有zsh、ksh等;

命令历史

history命令:

可以查看历史命令; 在用户的家目录下的 .bash_history文件中保存着之前敲过的命令, 默认最大存储1000条; history命令可以查询; 更改存储数: 更改变量HISTSIZE来达到更改存储数; 编辑文件vim /etc/profile

vim /etc/profile 修改HISTSIZE值,将HISTSIZE=1000改为10000 HISTSIZE=10000

更新缓存文件

source /etc/profile

查看变量值

[root@jinkai01 ~]# echo $HISTSIZE 10000

给history命令加上时间与日期 临时生效: HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

[root@jinkai01 ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " [root@jinkai01 ~]# echo $HISTTIMEFORMAT %Y/%m/%d %H:%M:%S [root@jinkai01 ~]# history | head -5 1 2020/08/14 17:03:43 ls -l 123/ 2 2020/08/14 17:03:43 chown :linuxprobe 123/ 3 2020/08/14 17:03:43 ls -l 4 2020/08/14 17:03:43 ls -l 123/ 5 2020/08/14 17:03:43 ls -ld 123

永久生效方法:

vim /etc/profile

增加变量定义 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " HISTSIZE=10000

更新缓存

[root@jinkai01 ~]# source /etc/profile [root@jinkai01 ~]# echo $HISTTIMEFORMAT %Y/%m/%d %H:%M:%S

执行下面一条命令后,在执行history 可以查看到命令历史的日期和用户

echo 'HISTTIMEFORMAT="%F %T `whoami` "' >> /etc/profile && source /etc/profile

查看所有用户的命令历史

cat /etc/passwd|for tmp in `awk -F: '$3>999 {print $1}'` ;do cat /home/$tmp/.bash_history; done

特殊命令

!!:表示执行上一条命令;

[root@jinkai01 ~]# pwd /root [root@jinkai01 ~]# !! pwd /root

!n:这里的n表示数值,,表示执行命令历史中的第n条指令;

[root@jinkai01 ~]# history | tail -5 1068 2020-08-14 17:55:55 root echo $HISTTIMEFORMAT 1069 2020-08-14 17:58:01 root history 1070 2020-08-14 17:59:28 root pwd 1071 2020-08-14 18:00:08 root history | tail -n 1072 2020-08-14 18:00:11 root history | tail -5 [root@jinkai01 ~]# !1070 pwd /root

!字符串:表示执行历史命令中最近一次一字符串开头的命令,必须两个以上;

[root@jinkai01 ~]# !pw pwd /root

命令补全和别名

命令补全

centos7支持命令参数补全; centos6只支持命令补全,不支持参数补全; 安装bash-completion包:

yum install -y bash-completion

安装后,重启生效;

别名

别名存放目录: 用户家目录下:~/.bashrc文件; 其他命令目录:/etc/profile.d/目录下的 当常用的命令与参数过长,我们可以定义一个别名来实现; 格式: alias 别名 = '源命令' systemctl restart network.service 将这条重启网卡服务的命令定义一个新的命令restartnet

[root@jinkai01 ~]# alias restartnet='systemctl restart network' [root@jinkai01 ~]# alias 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 restartnet='systemctl restart network' alias rm='rm -i' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' [root@jinkai01 ~]# restartnet 这里有一个错误,我赋值给network,写成network.server的时候,后续执行restartnet 会报错,显示为 network.server.server 多了一个server

取消别名 格式: unalias [自定义别名]

[root@jinkai01 ~]# unalias restartnet [root@jinkai01 ~]# alias 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' [root@jinkai01 ~]# restartnet -bash: restartnet: 未找到命令

通配符

*** 在bash下,可以用来匹配零个或多个字符; ?在bash下,?号表示匹配一个字符; [n-n]:n表示数值,n-n表示范围,例如:[0-3]表示范围0到3;**

实验1:使用*来查询;

[root@jinkai01 ~]# ls 1.txt 2.txt 3.txt 11.txt anaconda-ks.cfg wc [root@jinkai01 ~]# ls *.txt 1.txt 2.txt 3.txt 11.txt

实验2:使用?来查询;

[root@jinkai01 ~]# ls ?.txt 1.txt 2.txt 3.txt 无法查到11.txt 因为11是两个字符

实验3:使用[n-n]方括号范围来查询;[]内只取一个字符

[root@jinkai01 ~]# ls [1-3].txt 1.txt 2.txt 3.txt

实验4:查询范围数字0-9的.txt 小写字母.txt ;[]内不区分大小写

[root@jinkai01 ~]# ls [0-9a-z].txt 1.txt 2.txt 3.txt a.txt A.txt b.txt

实验5:指定选取几个字符

[root@jinkai01 ~]# ls [1,3,a].txt 1.txt 3.txt a.txt [root@jinkai01 ~]# ls [13a].txt 1.txt 3.txt a.txt [root@jinkai01 ~]# ls {1,3,a}.txt 1.txt 3.txt a.txt [root@jinkai01 ~]# ls {13a}.txt ls: 无法访问{13a}.txt: 没有那个文件或目录 [root@jinkai01 ~]# ls {1,11,3,a}.txt 11.txt 1.txt 3.txt a.txt [ ] 内可以分开写,也可以连着写;{ } 内必须以逗号分开写另外{ }内可以写书多个字符的文件。

输入输出重定向

.>:输出重定向,将一个字符串输出到一个文本中;输入两次后只计算后面一次; .>>:追加重定向,将一个字符串追加输入到一个文本中; <:输入重定向; 2>:错误重定向,将错误信息重定向到某个文本中; 2>>:错误追加重定向,将错误信息追加到某个文本中;

实验1:输出重定向;

[root@jinkai01 ~]# echo 111111 > 1.txt [root@jinkai01 ~]# cat 1.txt 111111

实验2:追加重定向;

[root@jinkai01 ~]# echo 22222 >> 1.txt [root@jinkai01 ~]# cat 1.txt 111111 22222

实验3:错误重定向;

[root@jinkai01 ~]# ls [12].txt aaa.txt >1.txt 2>2.txt [root@jinkai01 ~]# cat 1.txt 1.txt 2.txt [root@jinkai01 ~]# cat 2.txt ls: 无法访问aaa.txt: 没有那个文件或目录管道符

实验4:正确和错误输出到一个文件

[root@jinkai01 ~]# ls [12].txt aaa.txt >&1.txt [root@jinkai01 ~]# cat 1.txt ls: 无法访问aaa.txt: 没有那个文件或目录 1.txt 2.txt

符号|:管道符,将前面的命令交给后面的命令;

实验1:统计1.txt的段落长度;

cat 1.txt |wc -l [root@jinkai01 ~]# cat 1.txt | wc -l 3 [root@jinkai01 ~]#

实验2:查看1.txt文件,将文件中包含2的字符串打印出来;

cat 1.txt |grep '2' [root@jinkai01 ~]# cat 1.txt | grep 2 2.txt [root@jinkai01 ~]# cat 1.txt ls: 无法访问aaa.txt: 没有那个文件或目录 1.txt 2.txt

作业控制

当运行进程时,可以使它后台暂停(ctrl+z),然后使用bg命令后台活动,使用fg命令恢复它; 例如:vim 2.txt 使用ctrl+z暂停

[root@jinkai01 ~]# vim 2.txt

[4]+ 已停止 vim 2.txt

fg命令:

[root@jinkai01 ~]# fg vim 2.txt 恢复或切换到前台,多个后台可以使用fg [序列号] 来实现调回;

jobs命令: 查询中断作业,后台运行;

[root@jinkai01 ~]# jobs```

[1] 已停止 vim a [3]- 已停止 sleep 200 [4]+ 已停止 vim 2.txt

bg命令: 后台活动作业,可以让持续运行中的作业在后台继续保持运行中,但必须在暂停后才能bg切换到后台活动;

[root@jinkai01 ~]# sleep 1000 ^Z [5]+ 已停止 sleep 1000 [root@jinkai01 ~]# jobs [1] 已停止 vim a [4]- 已停止 vim 2.txt [5]+ 已停止 sleep 1000 [root@jinkai01 ~]# bg 5 [5]+ sleep 1000 & [root@jinkai01 ~]# jobs [1]- 已停止 vim a [4]+ 已停止 vim 2.txt [5] 运行中 sleep 1000 &~~