linux基礎(day23)

8.1 shell介紹

shell介紹

  • shell是一個命令解釋器,提供用戶和機器之間的交互
  • 支持特定語法,好比邏輯判斷、循環
  • 每一個用戶均可以有本身特定的shell
  • CentOS7默認shell爲bash (Bourne Agin Shell)
  • 還有zsh、ksh等——>用法和bash類似,可是細節仍是有必定差別
    • 查詢zsh包——>yum list |grep zsh
    • 查詢ksh包——>yum list |grep ksh

8.2 命令歷史

命令歷史

  • history //查看以前的命令
  • .bash_history //存放以前敲過的命令,在 /root/ 目錄下
  • 最大1000條 //默認參數值是1000條
  • 變量HISTSIZE
  • /etc/profile中修改 //在其中可編輯HISTSIZE參數
  • HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
  • 永久保存 chattr +a ~/.bash_history //增長隱藏權限,讓他人沒法刪除 !! 表示執行最後一條命令
  • !n 表示運行第幾條命令(n表示數字)
  • !echo 表示會在命令歷史裏面,倒着網上找第一個執行以 echo 開頭的命令

查看敲過的命令存放位置

  • 在咱們使用過的命令,會存放在用戶的家目錄下 /root/.bash_history
[root@localhost ~]# ls /root/.bash_history
/root/.bash_history
[root@localhost ~]# cat !$
cat /root/.bash_history
init 0
ping www.baidu.com
dhclient
ping www.baidu.com
yum install -y net-tools

等等等

查看以前敲過的命令

  • history //查看以前敲過的命令
[root@localhost ~]# history
    1  init 0
    2  ping www.baidu.com
    3  dhclient
    4  ping www.baidu.com
    5  yum install -y net-tools
    6  ifconfig

history命令中最多存放1000條歷史命令

  • history命令shell

    • 最多存放1000條
    • 是由環境變量HISTSIZE配置的
      • 這是系統內置的環境變量 HISTSIZE
        [root@localhost ~]# echo $HISTSIZE
        1000
        [root@localhost ~]#
        • 有時敲命令的時候,會出來更多的數值
          • 這是由於 尚未真正的寫入到文件中去,這些命令臨時存放在內存中
  • history -c //把當前內存裏面命令歷史給清空vim

[root@localhost ~]# history -c
[root@localhost ~]# history
    1  history
但不會清空 .bash_history 配置文件,僅僅是把歷史命令給清空

在敲完命令後,直接到配置文件中查看,會發現其中並無存在 這是由於僅存在內存中,只有在退出終端的時候,纔可以保存到配置文件中去centos

環境變量HISTSIZE在 /etc/profile 中配置

  • vim /etc/profile //在文件中修改環境變量的參數
    • 進入後 /HISTSIZE 搜索,並修改參數 HISTSIZE=5000
    • 在修改完參數後,要使參數生效,需
      • 方法1:須要重進一下終端
      • 方法二:執行 source /etc/profile
[root@localhost ~]# vim /etc/profile    //在裏面編輯文件,改變參數

改變參數後,能夠重啓終端,或者source /etc/profile,發現參數生效
[root@localhost ~]# source !$        //執行命令後,會發現HISTSIZE值變化了
source /etc/profile
[root@localhost ~]# echo $HISTSIZE
5000

記錄history查看歷史命令時間

  • 記錄命令使用的時間,給變量從新賦值,執行 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S" 便可
    • 指定變量的格式
[root@localhost ~]# history
    1  history
    2  vim /etc/profile
    3  yum provides "/*/vim"
    4  yum install -y vim-enhanced
    5  vim /etc/profile
    6  source /etc/profile
    7  echo $HISTSIZE
    8  HISTIMEFORMAT="%Y/%m/%d %H:%M:%S"
    9  history
[root@localhost ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
[root@localhost ~]# history
    1  2017/11/15 23:25:28history
    2  2017/11/15 23:35:08vim /etc/profile
    3  2017/11/15 23:35:29yum provides "/*/vim"
    4  2017/11/15 23:53:58yum install -y vim-enhanced
    5  2017/11/15 23:59:04vim /etc/profile
    6  2017/11/16 00:07:14source /etc/profile
    7  2017/11/16 00:07:31echo $HISTSIZE
    8  2017/11/16 00:13:45history
    9  2017/11/16 00:14:49HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
   10  2017/11/16 00:14:51history
   11  
[root@localhost ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
這個環境變量僅僅在當前窗口下的終端生效,在打開另外一個終端的時候,就會顯示空的
也就是說,系統默認這個環境變量是不存在的
    1. 若想這個環境變量一直生效,則須要編輯 /etc/profile
[root@hf-01 ~]# vim /etc/profile

進入配置文件中,在變量HISTSIZE下放入
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
而後 :wq 保存退出
[root@hf-01 ~]# source !$        
source /etc/profile
    1. 在編輯完保存退出後,再source /etc/profile
    1. 這時候,在打開另外一個終端
    • 執行echo $HISTTIMEFORMAT 會出現%Y/%m/%d %H:%M:%S
    • 這就表示執行成功,在其餘終端下也生效了

命令歷史永久保存

  • chattr +a ~/.bash_history //增長隱藏權限,讓別人沒法刪除
    • 增長權限後,就只能追加,而不能被刪除
[root@hf-01 ~]# chattr +a ~/.bash_history
[root@hf-01 ~]#

在運行不少命令後,未正常退出(exit或logout正常退出),直接關閉終端,那剛剛敲的命令就不會完整的保存到 .bash_history 中去bash

  • !! 表示執行最後一條命令
  • !n 表示運行第幾條命令(n表示數字)
  • !echo 表示會在命令歷史裏面,倒着網上找第一個執行以 echo 開頭的命令

8.3 命令補全和別名

命令補全及別名目錄概要

  • tab鍵,敲一下,敲兩下
  • 參數補全,安裝 bash-completion
  • alias別名給命令從新起個名字
  • 各用戶都有本身配置別名的文件 ~/.bashrc
  • ls /etc/profile.d/
  • 自定義的alias 放到 ~/.bashrc

命令補全

  • 在centos6中,命令補全僅支持命令自己,參數是不能補全的
  • 在centos7中,支持命令的參數補全
    • 須要安裝包bash-completion——>yum install -y bash-completion
    • 安裝完成後,須要重啓下虛擬機(reboot命令)纔可生效
    [root@hf-01 ~]# rpm -qa bash-completion        //查看包是否安裝完成
    bash-completion-2.1-6.el7.noarch
    • 再來測試,會看到一條命令行均可以補全

alias別名

    1. 如果命令較長,能夠設置別名
[root@hf-01 ~]# systemctl restart network.service        //重啓網絡服務
[root@hf-01 ~]# alias restartnet='systemctl restart network.service'
[root@hf-01 ~]# restartnet        //設置別名後,重啓網絡服務
[root@hf-01 ~]#
- 取消別名unalias
    - 在取消別名後,在輸入別名,就會提示未找到命令
[root@hf-01 profile.d]# unalias restartnet
[root@hf-01 profile.d]# restartnet
-bash: restartnet: 未找到命令
[root@hf-01 profile.d]#
    1. 直接輸入alias 會看到系統中全部的別名(包括本身自定義的alias)
[root@hf-01 ~]# 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 restarnet='systemctl restart network.service'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@hf-01 ~]#
    1. alias存放位置
    • 第一個是存放在用戶家目錄下的 .bashrc 文件中
    • 第二個是存放在 /etc/profile.d 目錄下的 colorls.sh 和 colorgrep.sh 腳本中定義的

8.4 通配符

  • ls *.txt
    • *表示通配,不分字符,也不分幾個
[root@hf-01 ~]# ls
111  123  1_heard.txt  1_sorft.txt  234  2.txt.bak  3.txt  anaconda-ks.cfg
[root@hf-01 ~]# ls *.txt        //以.txt結尾的文件都會列出來
1_heard.txt  1_sorft.txt  3.txt
[root@hf-01 ~]# ls *txt        //以txt結尾的文件都會列出來
1_heard.txt  1_sorft.txt  3.txt
[root@hf-01 ~]# ls *txt*        //包含txt的都會列出來
1_heard.txt  1_sorft.txt  2.txt.bak  3.txt
[root@hf-01 ~]# ls 1*        //只要1開頭的都會列出來
1_heard.txt  1_sorft.txt

111:

123:
[root@hf-01 ~]#
  • ls ?.txt
    • ?與* 相對比,? 表示一個任意的字符
    • 會看到(例子)只列出一個字符的.txt文件
[root@hf-01 ~]# touch 1.txt 2.txt
[root@hf-01 ~]# ls ?.txt
1.txt  2.txt  3.txt
[root@hf-01 ~]# touch a.txt bb.txt
[root@hf-01 ~]# ls ?.txt
1.txt  2.txt  3.txt  a.txt
[root@hf-01 ~]#
  • ls [0-9].txt
    • []這裏面能夠寫一個範圍
[root@hf-01 ~]# ls
111  1_heard.txt  1.txt  2.txt      3.txt            a.txt
123  1_sorft.txt  234    2.txt.bak  anaconda-ks.cfg  bb.txt
[root@hf-01 ~]# ls [0-3].txt
1.txt  2.txt  3.txt

    能夠把0,1,2,3這四個數字,任意一個都會知足這個條件,[]方括號中的字符只會取一個,就是「或者」的意思

[root@hf-01 ~]# ls [23].txt
2.txt  3.txt
[root@hf-01 ~]# ls [13].txt
1.txt  3.txt

在方括號中能夠寫範圍[0-9a-zA-Z]
  • ls {1,2}.txt
    • 也是或者的意思,這個範圍當中的一個
[root@hf-01 ~]# ls {1,2,3}.txt
1.txt  2.txt  3.txt
[root@hf-01 ~]# 

{1,2,3}.txt和[1-3].txt表達意思同樣,或者。只是在{}須要用  ,  逗號隔開

8.5 輸入輸出重定向

大於號,重定向

> 正確輸出
>> 追加劇定向
2> 錯誤重定向
2>> 錯誤追加劇定向

>+2>等於&>        表示結合了正確和錯誤
  • cat 1.txt > 2.txt
    • 一個大於號表示正確的輸出
    • 大於號>,表示前面的命令輸出,直接輸入到後面的文件中去
    • 就會把1.txt文件內容重定向到2.txt文件中,而2.txt以前的文件就會刪除掉,從新寫入1.txt文件內容
  • cat 1.txt >> 2.txt
    • 兩個大於號>>,就是追加,不會刪除2.txt文件內容,而是在原有的基礎上將1.txt文件內容寫入到2.txt文件中去
  • ls aaa.txt 2> err
    • 2大於號表示錯誤的輸出(錯誤信息)
    • 2> 表示它會把命令產生的錯誤信息指定輸入到一個文件中去
[root@hf-01 ~]# laaa
-bash: laaa: 未找到命令
[root@hf-01 ~]# laaa 2> a.txt
[root@hf-01 ~]# cat a.txt
-bash: laaa: 未找到命令
[root@hf-01 ~]#
  • ls aaa.txt 2>> err
  • ls [12].txt aaa.txt &> a.txt //正確和錯誤的輸出信息都輸出到a.txt中
[root@hf-01 ~]# ls [12].txt aaa.txt &> a.txt
[root@hf-01 ~]# cat a.txt
ls: 沒法訪問aaa.txt: 沒有那個文件或目錄
1.txt
2.txt
[root@hf-01 ~]#
  • 把正確和錯誤的輸出到文件中,方法一 ls [12].txt aaa.txt &>> a.txt
[root@hf-01 ~]# ls [12].txt aaa.txt &>> a.txt
[root@hf-01 ~]# cat !$
cat a.txt
ls: 沒法訪問aaa.txt: 沒有那個文件或目錄
1.txt
2.txt
ls: 沒法訪問aaa.txt: 沒有那個文件或目錄
1.txt
2.txt
  • 把正確和錯誤的輸出到文件中,方法二 ls [12].txt aaa.txt >1.txt 2>a.txt
[root@hf-01 ~]# ls [12].txt aaa.txt >1.txt 2>a.txt
[root@hf-01 ~]# cat 1.txt
1.txt
2.txt
[root@hf-01 ~]# cat a.txt
ls: 沒法訪問aaa.txt: 沒有那個文件或目錄
既能夠寫入一個文件中,也能夠分開寫入

小於號,重定向

  • 小於號< ,輸入重定向
  • wc -l < 1.txt //把1.txt文件內容輸入重定向到命令wc -l 中去
[root@hf-01 ~]# wc -l < 1.txt
2
[root@hf-01 ~]# 2.txt < 1.txt
-bash: 2.txt: 未找到命令
[root@hf-01 ~]#
  • 輸入重定向,左邊必須是命令,不支持文件輸入重定向到文件中的
相關文章
相關標籤/搜索