10月11日任务
8.1 shell介绍
8.2 命令历史
8.3 命令补全和别名
8.4 通配符
8.5 输入输出重定向
8.1 shell介绍
什么是shell
shell是一个命令解释器,提供用户和机器之间的交互
支持特定语法,比如逻辑判断、循环
每个用户都可以有自己特定的shell
CentOS7默认shell为bash(Bourne Agin Shell)
还有zsh/ksh等
8.2 命令历史
history命令
.bash_history
最大1000条
变量HISTSIZE
/etc/prefile中修改 #vim /etc/prefile 来修改变量HISTSIZE值
HISTTIMEFORMAT="%Y/%m/%d%H:%M:%S"
永久保存 chattr +a ~/.bash_history
!! #表示上一条命令
!n #表示第n条命令
!word #表示执行最后一条word命令
#查看历史命令
[root@centos6 ~]# history
#历史命令变量
[root@centos6 ~]# echo $HISTSIZE
1000
#history -c清空内存中的命令历史,但不能修改配置文件 .bash_history里的内容
[root@centos6 ~]# history -c
[root@centos6 ~]# history
1 history
[root@centos6 ~]# ls -l .bash_history
-rw-------. 1 root root 1043 Oct 10 17:43 .bash_history
#编辑配置文件/etc/profile中HISTSIZE值修改为5000保存退出,想立即生效需要运行source /etc/profile 。
[root@centos6 ~]# vim /etc/profile
[root@centos6 ~]# echo $HISTSIZE
1000
[root@centos6 ~]# source /etc/profile
[root@centos6 ~]# echo $HISTSIZE
5000
#编辑配置文件/etc/profile,在文档中HISTSIZE值下边一行添加HISTTIMEFORMAT="%Y/%m/%d% H:%M:%S",表示以年月日时间格式显示命令历史。
[root@centos6 ~]# vim /etc/profile
HISTSIZE=5000
HISTTIMEFORMAT="%Y/%m/%d% H:%M:%S"
[root@centos6 ~]# history
1 2018/10/11 10:04:15vi /etc/sysconfig/network-scripts/ifcfg-eth0
#给历史命令文件添加隐藏权限a,表示只能追加内容,不能删除。这样就可以永久保存历史记录。
[root@centos6 ~]# chattr +a ~/.bash_history
[root@centos6 ~]# lsattr .bash_history
-----a-------e- .bash_history
8.3 命令补全和别名
tab键,敲一下,敲两下
参数不全,安装bash-completion
alias别名给命令重新起个别名
各用户都有自己配置别名的文件 ~/.bashrc
ls /etc/profile.d/
自定义的alias放到~/.bashrc
8.4 通配符
ls *.txt # 表示通配
ls ?.txt #表示一个任意的数字或英文字符
ls [0-9].txt #表示0-9的数字范围中的一个
ls {1,2}.txt #表示1或者2范围中的一个
cat 1.txt >2.txt #>前边命令的结果输入到后边文件,后边文件原来的文件会被覆盖
cat 1.txt >>2.txt #>>前边命令的结果追加到后边文件
ls aaa.txt 2>err # 2>表示把错误的输出结果输出到err
ls aaa.txt 2>>err # 2>表示把错误的输出结果追加到err
wc -l <1.txt #< 表示输入重定向,不常用,了解就行
command >1.txt 2>&1
#范例 :&>表示把错误和正确的结果都输出到a文件 , 同理&>>表示追加。
[root@centos6 ~]# touch 1.txt
[root@centos6 ~]# ls
1.txt anaconda-ks.cfg install.log install.log.syslog
[root@centos6 ~]# ls 1.txt aaa.txt &> a.txt
[root@centos6 ~]# cat a.txt
ls: cannot access aaa.txt: No such file or directory
1.txt
#把正确和错误的输出结果分别保存到不同文件。
[root@centos6 ~]# ls 1.txt aaa.txt > a.txt 2>b.txt
[root@centos6 ~]# cat a.txt
1.txt
[root@centos6 ~]# cat b.txt
ls: cannot access aaa.txt: No such file or directory