shell介紹,命令輸入歷史、補全和別名,通配符,輸入輸出重定向

什麼是shell

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

命令歷史

  • history命令,查看命令的輸入歷史
  • .bash_history,命令輸入歷史保存文件,默認1000條
  • 變量HISTSIZE,在/etc/profile中修改;HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S ",歷史命令中時間格式化顯示
  • /etc/profile文件生效方式: 從新進終端或者source /etc/profile
  • 永久保存命令輸入歷史 chattr +a ~/.bash_history
  • !! # 執行上一條命令
  • !n # 執行命令歷史文件中第n條命令
  • !word 根據時間在命令歷史文件中重新到老找到的第一個word命令執行
  • history -c # 清空內存中緩存的命令,不會清空.bash_history文件的內容
  • 退出終端時會把內存中的命令寫到.bash_history文件中

命令補全及別名

  • tab鍵,敲一下會補全能惟一肯定的一個命令,敲兩下列出可選的命令
  • 命令參數補全,安裝bash-completion,安裝完成後須要重啓系統才能生效(CentOS7支持)
  • alias 別名 # 給命令從新起個名字
  • 各用戶都有本身配置別名的文件 ~/.bashrc,另外的別名配置文件在目錄/etc/profile.d/下
  • 自定義的alias放到 ~/.bashrc
[root@centos01 ~]# alias cat1='cat 1.txt'
[root@centos01 ~]# cat1
1
[root@centos01 ~]# unalias cat1
[root@centos01 ~]# cat1 # 取消別名
-bash: cat1: command not found

通配符、輸入輸出重定向

  • ls *.txt # *通配任意個字符
  • ls ?.txt # ?表示一個任意字符
  • ls [0-9].txt # [0-9] 取0到9之間的任意一個
  • ls {1,2}.txt # 列出1.txt和2.txt 等價於 [12].txt
  • cat 1.txt > 2.txt # 把命令cat 1.txt輸出的內容直接寫到2.txt(重寫前會清空2.txt) -cat 1.txt >> 2.txt # 把命令cat 1.txt輸出的內容追加寫到2.txt(重寫前不會清空2.txt)
  • ls aaa.txt 2>err # 命令ls aaa.txt產生的錯誤信息輸入到err文件
  • ls aaa.txt 2>>err # 命令ls aaa.txt產生的錯誤信息追加輸入到err文件
  • wc -l < 1.txt # 輸入重定向,左邊必須是命令
  • command > 1.txt 2>&1 # command執行的正確和錯誤結果輸出到1.txt,其中&1表示標準正確輸出目標,而命令command > 1.txt已經定義了正確輸出的文件1.txt,因此&1指代1.txt
[root@centos01 ~]# ls
1.txt  anaconda-ks.cfg  a.txt  d0917  link_test  s_link0.log  test.txt
[root@centos01 ~]# ls *txt
1.txt  a.txt  test.txt
[root@centos01 ~]# ls
1.txt  anaconda-ks.cfg  a.txt  d0917  link_test  s_link0.log  test.txt
[root@centos01 ~]# ls aaaa.txt 2>err.txt
[root@centos01 ~]# cat err.txt
ls: cannot access aaaa.txt: No such file or directory
[root@centos01 ~]#
[root@centos01 ~]# ls aaaa.txt >> err.txt
ls: cannot access aaaa.txt: No such file or directory
[root@centos01 ~]# cat err.txt
ls: cannot access aaaa.txt: No such file or directory
[root@centos01 ~]#
[root@centos01 ~]# ls
1.txt  anaconda-ks.cfg  a.txt  d0917  err.txt  link_test  s_link0.log  test.txt
[root@centos01 ~]# ls {1,2}.txt
ls: cannot access 2.txt: No such file or directory
1.txt
[root@centos01 ~]# ls {1,2}.txt > err.txt
ls: cannot access 2.txt: No such file or directory
[root@centos01 ~]# cat err.txt
1.txt
[root@centos01 ~]# ls {1,2}.txt &> err.txt
[root@centos01 ~]# cat err.txt
ls: cannot access 2.txt: No such file or directory
1.txt
[root@centos01 ~]# ls {1,2}.txt &> err.txt
[root@centos01 ~]# cat err.txt
ls: cannot access 2.txt: No such file or directory
1.txt
[root@centos01 ~]# ls {1,2}.txt &>> err.txt
[root@centos01 ~]# ls {1,2}.txt &>> err.txt
[root@centos01 ~]# cat err.txt
ls: cannot access 2.txt: No such file or directory
1.txt
ls: cannot access 2.txt: No such file or directory
1.txt
ls: cannot access 2.txt: No such file or directory
1.txt
[root@centos01 ~]# ls {1,2}.txt > res.txt 2>err.txt
[root@centos01 ~]# cat res.txt
1.txt
[root@centos01 ~]# cat err.txt
ls: cannot access 2.txt: No such file or directory

[root@centos01 ~]# ls {1,2}.txt > ttt.log 2>&1
[root@centos01 ~]# cat ttt.log
ls: cannot access 2.txt: No such file or directory
1.txt
相關文章
相關標籤/搜索