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
相關文章
相關標籤/搜索