用一條命令’pwd’判斷并證明系統命令的執行順序。

一、命令分類
    
    在Linux系統中命令分為兩類:
    
        1.內部命令:shell buildin 每次啟動系統都會重新加載入內存。
        2.外部命令:即某目錄下的文件,通過外部介質安裝的命令工具包,如通過yum、rpm等方式安裝,具體安裝路徑在$PATH下。

二、命令執行順序
    
    在命令執行過程中,命令形態主要分為4類:alias(別名命令)、內部命令、hash緩存命令和外部命令。

三、命令執行順序
    
    1. type command 查看是否是內部命令
    2. alias 定義別名 ualias取消別名
    3. enable command 啟用內部命令
              -n command 關閉內部命令
    4. hash 顯示hash緩存
            -d 刪除具體緩存命令路徑
    5. cp src dst/name 復制文件
    6. mv src dst  移動文件
    7. echo $PATH 顯示PATH的內容
    8. which -a  command  查看所有包含command的路徑
    9. rm 刪除文件
    

四、實例:‘pwd’即是內部命令又是外部命令,以‘pwd’為例實驗證明其執行順序

1.type命令判斷pwd命令是內部命令,新建alias pwd條目為ls命令并執行,結果為列出當前目錄文件,說明alias優先于內部命令。

    [root@h(Brad) etc]#type pwd
    pwd is a shell builtin
    [root@h(Brad) ~]#alias -p pwd=’ls’
    [root@h(Brad) ~]#pwd
    123      723.txt          dead.letter  Documents  install.log
    
2.禁用內部命令pwd,清空hash,執行pwd仍然顯示ls結果,說明不論內部命令是否啟用,都會執行alias。

    [root@h(Brad) etc]#enable -n pwd
    [root@h(Brad) ~]#hash -r
    [root@h(Brad) ~]#pwd
    123      723.txt          dead.letter  Documents  install.log
    
3.刪除aliaspwd,使用pwd命令,仍然有效,并且判斷為外部命令,hash路徑在/bin/pwd。說明相比外部命令,系統優先使用內部命令。

    [root@h(Brad) ~]#unalias pwd
    [root@h(Brad) etc]#pwd
    /etc
    [root@h(Brad) etc]#type pwd
    pwd is hashed (/bin/pwd)
    
4.刪除hash -d pwd,用hash -p命令創建一條pwd的hash,cp /sbin/ifconfig到/usr/sbin/pwd執行pwd結果為查看網絡信息。說明執行的是/usr/sbin/pwd,hash路徑優先。

    [root@h(Brad) etc]#hash -d pwd
    [root@h(Brad) ~]#cp /sbin/ifconfig /usr/sbin/pwd
    [root@h(Brad) ~]#hash -p /usr/sbin/pwd pwd
    [root@h(Brad) ~]#pwd
    eth0      Link encap:Ethernet  HWaddr 00:0C:29:03:75:7F  
          inet addr:172.16.253.154  Bcast:172.16.255.255  Mask:255.255.0.0
    
    
5.顯示PATH路徑

    [root@h(Brad) etc]#echo $PATH
    /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
    
6.hash -d 刪除pwd條目
  

    [root@h(Brad) etc]#hash
    hits    command
      20    /bin/grep
       1    /bin/pwd
    [root@h(Brad) etc]#hash -d pwd
    [root@h(Brad) etc]#hash
    hits    command
      20    /bin/grep
       2    /usr/sbin/groupadd

7.move pwd到/usr/sbin并執行,可以執行,并hash /usr/bin,說明在PATH的其他目錄中也可以執行,并hash。      

    [root@h(Brad) etc]#mv /bin/pwd /usr/sbin
    [root@h(Brad) etc]#pwd
    /etc
    [root@h(Brad) etc]#hash
    hits    command
      20    /bin/grep
       1    /usr/sbin/pwd
       
8.刪除hash中pwd ,cp /sbin/ifconfig到/bin/pwd并執行,執行pwd結果為查看網絡信息。說明執行的是/bin/pwd,在PATH中左面目錄為優先執行目錄。

    [root@h(Brad) etc]#hash -d pwd
    [root@h(Brad) etc]#cp /sbin/ifconfig /bin/pwd
    [root@h(Brad) ~]#which -a pwd                   
    /bin/pwd
    /usr/sbin/pwd[root@h(Brad) etc]#hash -d pwd
    [root@h(Brad) etc]#pwd
        eth0      Link encap:Ethernet  HWaddr 00:0C:29:03:75:7F  
          inet addr:172.16.253.154  Bcast:172.16.255.255  Mask:255.255.0.0
          
9.rm刪除/bin/pwd,執行pwd顯示找不文件,說明相比PATH順序,優先讀取hash中的緩存目錄命令。

    [root@h(Brad) ~]#rm /bin/pwd
    rm: remove regular file `/bin/pwd’? y
    [root@h(Brad) etc]#pwd
    -bash: /bin/pwd: No such file or directory
    
五、結論:系統執行命令的優先順序依次為:
alias——> shell builtin——> hash ——> PATH

原創文章,作者:manbinn,如若轉載,請注明出處:http://www.www58058.com/86057

(1)
manbinnmanbinn
上一篇 2017-09-02
下一篇 2017-09-03

相關推薦

  • rpm詳解

    rpm詳解 rpm詳解 rpm使用方法 rpm起源 rpm是什么 rpm命名格式 rpm優缺點 rpm獲取注意 rpm命令使用 rpm起源  由于在linux中安裝應用程序需要源碼包編譯安裝,對于非專業人員而言難度太大,因而出現一種將源碼編譯好的二進制程序,庫文件,配置文件,幫助文件等打包成一個或多個特定格式的程序包,而管理這類包的工具之一,則稱為…

    Linux干貨 2016-12-23
  • 計算機編程簡史圖

       這個圖片太經典了,本來想翻譯的,后來覺得這么經典的圖片可能早已被人翻譯了,簡單的Google一下,果然有人翻譯了。那我就把英文版和中文版都轉過來吧。我們可以看到,其中很大一部分人都和Unix有著不解之緣(參見《Unix傳奇上篇,Unix傳奇下篇》) 英文原版 中文翻譯版 什么也不說了,直接上圖(圖片比較大,單擊圖片看大圖) 計算機編…

    Linux干貨 2015-04-02
  • CentOS系統啟動流程、selinux、Systemd剖析

    交互式登錄配置文件讀取順序: /etc/profile –> /etc/profile.d/*.sh –> ~/.bash_profile –> ~/.bashrc –> /etc/bashrc 非交互式登錄配置文件讀取順序: ~/.bashrc –> /etc/bas…

    Linux干貨 2018-03-04
  • Linux 第八天: Linux文本編輯器vim

    Linux 第八天: (08月09日) Linux文本編輯器vim       sed 行編輯器 stream EDitor sed -n 不輸出模式空間內容的自動打印sed -e 多點編輯sed -f /PATH/TO/SCRIPTFILEsed -i 原處編輯   script單地址 # 指定的行/pattern/…

    Linux干貨 2016-08-11
  • 馬哥教育網絡21期+第六周練習博客

    請詳細總結vim編輯器的使用并完成以下練習題 1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#; [root@localhost named]# cp /etc/rc.d/rc.sysinit /tmp/ [root@loc…

    Linux干貨 2016-08-15
  • linux防火墻介紹

    一、前言firewall(防火墻):工作在網絡進入或者流包,進出的網絡數據包進行一定的規則進行檢查過濾系統。包括iptables和netfilter組件。iptables 是與 Linux 內核集成的 IP 信息包過濾系統。如果 Linux 系統連接到因特網或 LAN、服務器或連接 LAN 和因特網的代理服務器, 則該系統系統中更好地控制 IP 信息包過濾和…

    2017-04-30
欧美性久久久久