shell介绍

1.其他的shell:zsh,ksh

[root@weix-01 ~]# yum list | grep zsh
zsh.x86_64                                5.0.2-28.el7                 installed
autojump-zsh.noarch                       22.3.0-3.el7                 epel
zsh-html.x86_64                           5.0.2-28.el7                 base
zsh-lovers.noarch                         0.9.0-1.el7                  epel
[root@weix-01 ~]# yum list | grep ksh
ksh.x86_64                                20120801-34.el7              base
mksh.x86_64                               46-5.el7                     base
python-XStatic-Rickshaw.noarch            1.5.0.0-4.el7                epel
python-moksha-common.noarch               1.2.3-2.el7                  epel
python-moksha-hub.noarch                  1.5.3-2.el7                  epel
python-moksha-wsgi.noarch                 1.2.2-2.el7                  epel

命令历史

1.存放地址:

[root@weix-01 ~]# ls /root/.bash_history
/root/.bash_history
[root@weix-01 ~]# cat !$
cat /root/.bash_history
which ls
echo $PATH
whereis ls
yum installl -y locate
yum installl -y mlocate
yum install -y mlocate
locate

2.最多储存1000条,由标量控制:

[root@weix-01 ~]# echo $HISTSIZE
1000

3.history -c命令:

清空内存里面命令历史,但是配置文件里面无法清空

4.退出终端时,内存中保存的历史命令才会写到配置文件中去 5.修改变量HISTSIZE:

[root@weix-01 ~]# vi /etc/profile

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth

6.改完后需要执行以下命令才生效:

[root@weix-01 ~]# source /etc/profile

7.让保存的历史命令有具体时间日期: 临时修改

[root@weix-01 ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
[root@weix-01 ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
[root@weix-01 ~]# history
    1  2018/01/10 15:51:53history
    2  2018/01/10 15:52:05cat .bash_history
    3  2018/01/10 15:53:01history
    4  2018/01/10 15:56:06vi /etc/profile
    5  2018/01/10 15:57:46echo $HISTSIZE
    6  2018/01/10 16:00:59HISTTIMEFORMAT=%Y%m%d %H:%M:%S
    7  2018/01/10 16:01:29HISTTIMEFORMAT=%Y/%m/%d %H:%M:%S
    8  2018/01/10 16:01:55HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
    9  2018/01/10 16:02:13echo $HISTTIMEFORMAT
   10  2018/01/10 16:02:19history

永久修改 修改配置文件profile:将变量修改到配置文件

vim /etc/profile


HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
[root@weix-01 ~]# echo $H

然后重新登录终端 8.!!表示上一条命令 9.!n 表示运行第多少条命令

  308  2018/01/10 17:21:54source /etc/profile
  309  2018/01/10 17:27:01ls
  310  2018/01/10 17:27:41history
[root@weix-01 ~]# !309
ls
111  1.txt   2_hard.txt  2.txt.bak.bak  4.txt              apr-1.4.5
123  1.txt~  2_soft.txt  3.txt          anaconda-ks.cfg.1  apr-1.4.5.tar.gz

10.!命令:在命令历史里倒着寻找第一个以该命令开头的命令。

命令补全及别名

1.CentOS7支持补全命令选项:需要安装新的包bash-completion

[root@weix01 ~]# yum install -y bash-completion
已加载插件:fastestmirror
file:///mnt/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /mnt/repodata/repomd.xml"
正在尝试其它镜像。
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 bash-completion.noarch.1.2.1-6.el7 将被 安装
--> 解决依赖关系完成

安装后重启系统reboot 2.alias别名

[root@weix01 ~]# alias restartnet='systemctl restart network.service'        #建立别名
[root@weix01 ~]# restartnet

查看有哪些别名

[root@weix01 ~]# 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.service'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

3.存放alias的地方:在以下两个地方

[root@weix01 ~]# vi .bashrc
[root@weix01 ~]# cd /etc/profile.d

4.取消别名:unalias

[root@weix01 ~]# unalias restartnet
[root@weix01 ~]# res
reset       resize2fs   resizecons  resizepart  restorecon  
[root@weix01 ~]# res
reset       resize2fs   resizecons  resizepart  restorecon  
[root@weix01 ~]# restartnet
-bash: restartnet: 未找到命令

通配符、输入输出重定向

*  表示任意数量任意字符
?代表任意一个字符
[1-3].txt      代表一个范围
{1,2}.txt         代表一个范围
>        把前一个命令的输出输入到后面文件中去,会把后面文件内容清空重写
>>          追加,不会删除后面文件内容
2>         把命令产生的错误信息输入到一个文件中去
[root@weix01 ~]# lsaaa 2> 2.txt
[root@weix01 ~]# cat 2.txt
-bash: lsaaa: 未找到命令

2>>      错误追加重定向
[root@weix01 ~]# lsaaa 2>> 2.txt
[root@weix01 ~]# cat 2.txt
-bash: lsaaa: 未找到命令
-bash: lsaaa: 未找到命令

&>          把错误和正确信息都输入到一个文件
[root@weix01 ~]# ls [12].txt aaa.txt &>a.txt
[root@weix01 ~]# cat a.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
2.txt

&>>       把错误和正确信息都追加到一个文件
可以把正确和错误的分开存放
[root@weix01 ~]# ls [12].txt aaa.txt >1.txt 2>a.txt
[root@weix01 ~]# cat 1.txt
2.txt
[root@weix01 ~]# cat a.txt
ls: 无法访问aaa.txt: 没有那个文件或目录

<        输入重定向
[root@weix01 ~]# wc -l 2.txt > 1.txt
[root@weix01 ~]# cat 1.txt
2 2.txt
[root@weix01 ~]# wc -l < 1.txt
1
[root@weix01 ~]# wc -l 1.txt
1 1.txt