shell

shell.1.1 bash特性:命令历史、命令与路径补全

一、命令历史

1> shell进程会在其会话中保存此前用户提交执行过的命令;
2> 定制history的功能,可通过环境变量实现;
环境变量:
1> HISTSIZE : shell 进程可保留的命令历史的条数;
2> HISTFILE : 持久保存命令历史的文件;
3> HISTFILESIZE : 命令历史文件的大小;

PS :
> .bash_history 文件中记录的历史命令,默认只能存储1000条;
> 系统启动后会加载上面文件内的历史命令到内存中;
> 用history命令查看到的历史命令包括2部分,.bash_history 文件中记录的历史命令 + 用户新执行的命令;
> 用户登出shell时,内存中的历史命令会写入文件 .bash_history 中,但只能保存默认的1000条,超出数额时,挤掉旧的历史命令;

[root@localhost ~]# echo $HISTSIZE
1000
[root@localhost ~]# echo $HISTFILE
/root/.bash_history
[root@localhost ~]# 
[root@localhost ~]# echo $HISTFILESIZE
1000
[root@localhost ~]#

history 命令:
命令格式:
history [-c] [-d offset] [n] or
history -anrw [filename] or
history -ps arg [arg…]

history -c : 清空内存所有历史命令
history -d NUMBER : 清空内存中指定条数的历史命令(用history命令查看到的历史命令)
history -d 偏移量 n : 删除指定行以下的 n 个历史命令,偏移量指定是历史命令的行号,删除的是从’偏移量行号’开始的 n 行;

举例:删除的是第四行开始的1行
[root@localhost ~]# history 
    1  history 
    2  more .bash_history 
    3  history 
    4  history 310 2
    5  history -d 310 2
    6  history 
    7  ls
    8  history 
[root@localhost ~]# history -d 4 1
[root@localhost ~]# history 
    1  history 
    2  more .bash_history 
    3  history 
    4  history -d 310 2
    5  history 
    6  ls
    7  history 
    8  history -d 4 1
    9  history 
[root@localhost ~]#

history -a : 将内存中的历史命令追加到文件 .bash_history 中;
history -n : 从文件 .bash_history 中读取未被读取的行到内存中;
history -r : 从文件 .bash_history 中读取所有历史命令到内存;
history -w : 将内存中的历史命令覆盖到文件 .bash_history 中;
history N : 显示最新的N条历史命令;

[root@localhost ~]# history 8
  311  cd mysql-5.7.20/
  312  make clean
  313  make
  314  ip a
  315  make install
  316  exit
  317  history 
  318  history 8
[root@localhost ~]#

调用命令历史列表中的命令
~]# !N : 再次执行历史列表中的某条命令(用命令history命令查看到的存储在内存的历史命令);
~]# !! : 再次执行上一条命令;
~]# !STRING : 再次执行命令历史中最近一个以 ‘STRING’ 开头的命令;

[root@localhost ~]# history 
...........
  310  ll
  311  cd mysql-5.7.20/
  312  make clean
  313  make
  314  ip a
  315  make install
  316  exit
  317  history 
  318  history 8
  319  history 
  320  pwd
  321  ls
  322  ll
  323  history 
  324  top
  325  history 

[root@localhost ~]# !320
pwd
/root
[root@localhost ~]# !!
pwd
/root
[root@localhost ~]# !p
pwd
/root
[root@localhost ~]#

调用上一条命令的最后一个参数
按 ESC 键后松开,再按 . 号;或者直接按 !$ ;

举例:输入 'file' 命令后,按 ESC 键后松开,再按 . 号,就可调出上条命令的最后一个参数。
[root@localhost ~]# ls /etc/sysconfig/network-scripts/ifcfg-ens33 
/etc/sysconfig/network-scripts/ifcfg-ens33
[root@localhost ~]# 
[root@localhost ~]# file /etc/sysconfig/network-scripts/ifcfg-ens33
/etc/sysconfig/network-scripts/ifcfg-ens33: ASCII text
[root@localhost ~]# 
[root@localhost ~]# file !$
file /etc/sysconfig/network-scripts/ifcfg-ens33
/etc/sysconfig/network-scripts/ifcfg-ens33: ASCII text
[root@localhost ~]#

控制命令历史记录的方式
由环境变量控制 : HISTCONTROL
centos 7 系统中有3个取值:
> ignoredups : 忽略重复的命令(连续相同的命令才算是重复的),centos 7 的默认取值;
> ignorespace: 忽略以空白字符开头的命令;
> ignoreboth : 以上2个同时生效;
PS : 修改变量值的操作如下 HISTCONTROL=’VALUE’ ,这个操作只对当前shell有效;

[root@localhost ~]# echo $HISTCONTROL
ignoredups
[root@localhost ~]#

二、命令补全

1、shell程序在接收到用户执行命令的请求并分析完成之后,最左侧的字符串会被当作命令处理;
2、命令查找机制
1> 查找内部命令;
2> 根据PATH环境变量中设定的目录,自左而右逐个搜索目录下的命令文件名;

>> 给定的大头字符串如果能唯一标识某命令程序文件,按tab键后可直接补全命令;
>> 给定的大头字符串如果不能唯一标识某命令程序文件,双击tab键,会给出列表;

三、路径补全
>> 在给定的起始路径下,以对应路径下的打头字符串来逐一匹配起始路径下的每个文件;

Leave a Reply

Your email address will not be published. Required fields are marked *