shell介紹、history歷史命令及配置文件、tab補全、通配符和輸出重定向

10月11日任務shell

8.1 shell介紹vim

8.2 命令歷史centos

8.3 命令補全和別名bash

8.4 通配符spa

8.5 輸入輸出重定向ip

 

8.1 shell介紹內存

什麼是shell文檔

  • shell是一個命令解釋器,提供用戶和機器之間的交互
  • 支持特定語法,好比邏輯判斷、循環
  • 每一個用戶均可以有本身特定的shell
  • CentOS7默認shell爲bash(Bourne Agin  Shell)
  • 還有zsh/ksh等

 

8.2 命令歷史io

  • 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命令 

#查看歷史命令zsh

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