5-3 8 shell介绍 命令历史 补全 别名 通配符 重定向

8.1 shell介绍

什么是shellhtml

  • shell是一个命令解释器,提供用户和机器之间的交互(命令行终端)
    • 脚本shell是一种表现形式
    • passwd中最后一段便是用户的shell
  • shell支持特定语法,好比逻辑判断、循环
  • 每一个用户均可以有本身特定的shell
  • CentOS7默认shell为bash(Bourne Again Shell)
  • 还有zsh ksh等。大致同样,细节有差别。
    • 默认没有安装
[root@lixiang01 ~]# yum list |grep zsh
autojump-zsh.noarch                     22.3.0-3.el7                   epel     
zsh.x86_64                              5.0.2-25.el7_3.1               updates  
zsh-html.x86_64                         5.0.2-25.el7_3.1               updates  
zsh-lovers.noarch                       0.9.0-1.el7                    epel     
[root@lixiang01 ~]# yum list |grep ksh
ksh.x86_64                              20120801-26.el7                base     
mksh.x86_64                             46-5.el7                       base

8.2 命令历史

[root@ax-01 ~]# history
  • history命令,记录历史命令
  • 用户的家目录下的.bash_history,记录上一次正常退出后的命令历史
    • 非正常退出保存不完整

修改命令历史保存数量shell

[root@ax-01 ~]# echo $HISTSIZE  //默认命令历史最大1000条
1000
[root@ax-01 ~]# vim /etc/profile  //能够在此调整环境变量

[root@ax-01 ~]# source !$
source /etc/profile
[root@ax-01 ~]# echo $HISTSIZE
5000
  • 由环境变量控制$HISTSIZE保存最大保存命令。运行时内存能够保存多于1000条
  • history -c 能够清空内存中的命令历史
  • 退出终端后保存至.bash_history

修改命令历史显示格式vim

[root@ax-01 ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
[root@ax-01 ~]# history
    2  2017/08/30 09:51:25echo $HISTSIZE
    3  2017/08/30 09:51:44vim /etc/profile
    4  2017/08/30 09:53:37echo $HISTSIZE
[root@ax-01 ~]# !vim
vim /etc/profile

[root@ax-01 ~]# source /etc/profile
[root@ax-01 ~]# !echo
echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
  • HISTTIMEFORMAT="%Y/%m/%d % H:%M:%S" //须要注意大小写 修改profile永久生效

命令历史永久保存bash

[root@ax-01 ~]# chattr +a .bash_history 
[root@ax-01 ~]# lsattr .bash_history 
-----a-------e-- .bash_history
  • 不受环境变量 HISTSIZE影响

叹号常见用法命令行

[root@ax-01 etc]# cd /etc/yum.repos.d/
[root@ax-01 yum.repos.d]# cd ..
[root@ax-01 etc]# !!  再次执行上一条命令
cd ..
[root@ax-01 /]#
[root@ax-01 /]# cd
[root@ax-01 ~]# history | tail -n4
 1051  cd /etc/yum.repos.d/
 1052  cd ..
 1053  cd
 1054  history | tail -n4
[root@ax-01 ~]# !1051  //照history编号运行
cd /etc/yum.repos.d/
  • !! // 最后一条命令
  • !n // n是数字 按照history编号运行
  • !xx //执行最近一次以xx字符开头的命令

8.3 命令补全和别名

  • tab键命令补全,
    • 敲一下若是后续字符惟一则自动补全
    • 若是不惟一,敲两下看可选后续字符

参数补全rest

[root@ax-01 ~]# yum install -y bash-completion  //安装参数补全包,须要重启
[root@ax-01 ~]# reboot
[root@ax-01 ~]# systemctl restart network
network-online.target  network.service
  • CentOS7新加入的支持参数补全。很实用

alias别名code

[root@ax-01 ~]# alias renet='systemctl restart network'
[root@ax-01 ~]# echo "alias renet='systemctl restart network'" >> .bashrc
[root@ax-01 ~]# unalias renet 取消
[root@ax-01 ~]# vim .bashrc 删除定义别名
  • 部分系统别名在/etc/profile.d/目录下的文件里
  • 每一个用户都有本身的别名配置文件

8.4 通配符

  • ls *.txt //表示任意
  • ls ?.txt //表示任意1个
  • ls [0-9].txt //表示范围中任意1个
  • ls {1,3}.txt //同上

实验htm

[root@lixiang01 ~]# ls *txt*
1_heard.txt.bak  1.txt  22.txt  2.txt.bak  test.txt  word1.txt  记录.txt
[root@lixiang01 ~]# ls 1*
123.tar.bz2  1_heard.txt.bak  1.txt

111:
22.txt
[root@lixiang01 ~]# ls ?.txt
1.txt
[root@lixiang01 ~]# touch 2.txt
[root@lixiang01 ~]# touch 3.txt
[root@lixiang01 ~]# ls ?.txt
1.txt  2.txt  3.txt
[root@lixiang01 ~]# ls [0-3].txt
1.txt  2.txt  3.txt
[root@lixiang01 ~]# ls [123].txt
1.txt  2.txt  3.txt
[root@lixiang01 ~]#  ls [0-9].txt
1.txt  2.txt  3.txt
[root@lixiang01 ~]#  ls [0-9a-zA-Z].txt
1.txt  2.txt  3.txt
[root@lixiang01 ~]# touch d.txt
[root@lixiang01 ~]#  ls [0-9a-zA-Z].txt
1.txt  2.txt  3.txt  d.txt
[root@lixiang01 ~]# ls {1,3,d}.txt
1.txt  3.txt  d.txt

8.5 输入输出重定向

> 输出重定向
>> 追加
2> 错误重定向,支持追加2>>
&> 所有重定向,支持追加&>>

区分输出正确与错误信息内存

[root@ax-01 ~]# cat /etc/passwd nosuchthing.txt > right.log 2> err.log
[root@ax-01 ~]# cat err.log 
cat: nosuchthing.txt: No such file or directory
相关文章
相关标签/搜索