日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

理解 Linux 下 Shell 命令的不同分類及它們的用法

 孤獨一兵 2016-09-26

當你打算真正操縱好你的 Linux 系統(tǒng),沒有什么能比命令行界面更讓你做到這一點。為了成為一個 Linux 高手,你必須能夠理解 Shell 命令的不同類型[1],并且會在終端下正確的使用它們。

在 Linux 下,命令有幾種類型,對于一個 Linux 新手來說,知道不同命令的意思才能夠高效和準確的使用它們。因此,在這篇文章里,我們將會遍及各種不同分類的 Linux Shell 命令。

需要注意一件非常重要的事:命令行界面和 Shell 是不同的,命令行界面只是為你提供一個訪問 Shell 的方式。而 Shell ,它是可編程的,這使得它可以通過命令與內(nèi)核進行交流。

理解 Linux 下 Shell 命令的不同分類及它們的用法

下面列出了 Linux 下命令的不同種類:

1. 程序可執(zhí)行文件(文件系統(tǒng) 中的命令)

當你執(zhí)行一條命令的時候,Linux 通過從左到右搜索存儲在 $PATH環(huán)境變量中的目錄來找到這條命令的可執(zhí)行文件。

你可以像下面這樣查看存儲在 $PATH中的目錄:

  1. $ echo $PATH

  2. /home/aaronkilik/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

在上面的命令中,目錄 /home/aaronkilik/bin將會被首先搜索,緊跟著是/usr/local/sbin,然后一直接著下去。在搜索過程中,搜索順序是至關(guān)重要的。

比如在 /usr/bin目錄里的文件系統(tǒng)中的命令:

  1. $ ll /bin/

示例輸出:

  1. total 16284

  2. drwxr-xr-x 2 root root 4096 Jul 31 16:30 ./

  3. drwxr-xr-x 23 root root 4096 Jul 31 16:29 ../

  4. -rwxr-xr-x 1 root root 6456 Apr 14 18:53 archdetect*

  5. -rwxr-xr-x 1 root root 1037440 May 17 16:15 bash*

  6. -rwxr-xr-x 1 root root 520992 Jan 20 2016 btrfs*

  7. -rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfs-calc-size*

  8. lrwxrwxrwx 1 root root 5 Jul 31 16:19 btrfsck -> btrfs*

  9. -rwxr-xr-x 1 root root 278376 Jan 20 2016 btrfs-convert*

  10. -rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfs-debug-tree*

  11. -rwxr-xr-x 1 root root 245368 Jan 20 2016 btrfs-find-root*

  12. -rwxr-xr-x 1 root root 270136 Jan 20 2016 btrfs-image*

  13. -rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfs-map-logical*

  14. -rwxr-xr-x 1 root root 245368 Jan 20 2016 btrfs-select-super*

  15. -rwxr-xr-x 1 root root 253816 Jan 20 2016 btrfs-show-super*

  16. -rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfstune*

  17. -rwxr-xr-x 1 root root 245368 Jan 20 2016 btrfs-zero-log*

  18. -rwxr-xr-x 1 root root 31288 May 20 2015 bunzip2*

  19. -rwxr-xr-x 1 root root 1964536 Aug 19 2015 busybox*

  20. -rwxr-xr-x 1 root root 31288 May 20 2015 bzcat*

  21. lrwxrwxrwx 1 root root 6 Jul 31 16:19 bzcmp -> bzdiff*

  22. -rwxr-xr-x 1 root root 2140 May 20 2015 bzdiff*

  23. lrwxrwxrwx 1 root root 6 Jul 31 16:19 bzegrep -> bzgrep*

  24. -rwxr-xr-x 1 root root 4877 May 20 2015 bzexe*

  25. lrwxrwxrwx 1 root root 6 Jul 31 16:19 bzfgrep -> bzgrep*

  26. -rwxr-xr-x 1 root root 3642 May 20 2015 bzgrep*

2. Linux 別名

這些是用戶定義的命令,它們是通過 shell 內(nèi)置命令 alias創(chuàng)建的,其中包含其它一些帶有選項和參數(shù)的 shell 命令。這個意圖主要是使用新穎、簡短的名字來替代冗長的命令。

創(chuàng)建一個別名的語法像下面這樣:

  1. $ alias newcommand='command -options'

通過下面的命令,可以列舉系統(tǒng)中的所有別名:

  1. $ alias -p

  2. alias alert='notify-send --urgency=low -i '$([ $? = 0 ] && echo terminal || echo error)' '$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')''

  3. alias egrep='egrep --color=auto'

  4. alias fgrep='fgrep --color=auto'

  5. alias grep='grep --color=auto'

  6. alias l='ls -CF'

  7. alias la='ls -A'

  8. alias ll='ls -alF'

  9. alias ls='ls --color=auto'

要在 Linux 中創(chuàng)建一個新的別名,仔細閱讀下面的例子。

  1. $ alias update='sudo apt update'

  2. $ alias upgrade='sudo apt dist-upgrade'

  3. $ alias -p | grep 'up'

理解 Linux 下 Shell 命令的不同分類及它們的用法

然而,上面這些我們創(chuàng)建的別名只能暫時的工作,當經(jīng)過下一次系統(tǒng)啟動后它們不再工作。你可以像下面展示的這樣在 '.bashrc' 文件中設(shè)置永久別名。

理解 Linux 下 Shell 命令的不同分類及它們的用法

添加以后,運行下面的命令來激活:

  1. $ source ~/.bashrc

3. Linux Shell 保留字

在 shell 程序設(shè)計中,if、then、fi、for、whilecase、esac、else、until以及其他更多的字都是 shell 保留字。正如描述所暗示的,它們在 shell 中有特殊的含義。

你可以通過使用下面展示的 type命令來列出所有的 shell 關(guān)鍵字:

  1. $ type if then fi for while case esac else until

  2. if is a shell keyword

  3. then is a shell keyword

  4. fi is a shell keyword

  5. for is a shell keyword

  6. while is a shell keyword

  7. case is a shell keyword

  8. esac is a shell keyword

  9. else is a shell keyword

  10. until is a shell keyword

4. Linux shell 函數(shù)

一個 shell 函數(shù)是一組在當前 shell 內(nèi)一起執(zhí)行的命令。函數(shù)有利于在 shell 腳本中實現(xiàn)特殊任務。在 shell 腳本中寫 shell 函數(shù)的傳統(tǒng)形式是下面這樣:

  1. function_name {

  2. command1

  3. command2

  4. ......

  5. }

或者像這樣:

  1. function function_name {

  2. command1

  3. command2

  4. ......

  5. }

讓我們看一看如何在一個名為 shell_functions.sh 的腳本中寫 shell 函數(shù)。

  1. #!/bin/bash

  2. #write a shell function to update and upgrade installed packages

  3. upgrade_system{

  4. sudo apt update;

  5. sudo apt dist-upgrade;

  6. }

  7. #execute function

  8. upgrade_system

取代通過命令行執(zhí)行兩條命令:sudo apt updatesudo apt dist-upgrade,我們在腳本內(nèi)寫了一個像執(zhí)行一條單一命令一樣來執(zhí)行兩條命令的 shell 函數(shù) upgrade_system。

保存文件,然后使腳本可執(zhí)行。最后像下面這樣運行 shell 函數(shù):

  1. $ chmod +x shell_functions.sh

  2. $ ./shell_functions.sh

理解 Linux 下 Shell 命令的不同分類及它們的用法

5. Linux Shell 內(nèi)置命令

這些是在 shell 中內(nèi)置的 Linux 命令,所以你無法在文件系統(tǒng)中找到它們。這些命令包括pwd、cd、bgalias、history、type、source、read、exit等。

你可以通過下面展示的 type命令來列出或檢查 Linux 內(nèi)置命令:

  1. $ type pwd

  2. pwd is a shell builtin

  3. $ type cd

  4. cd is a shell builtin

  5. $ type bg

  6. bg is a shell builtin

  7. $ type alias

  8. alias is a shell builtin

  9. $ type history

  10. history is a shell builtin

學習一些 Linux 內(nèi)置命令用法:

結(jié)論

作為一個 Linux 用戶,知道你所運行的命令類型是很重要的。我相信,通過上面明確、簡單并且易于理解的解釋,包括一些相關(guān)的說明,你可能對 “Linux 命令的不同種類[5]”有了很好的理解。

你也可以在下面的評論區(qū)提任何問題或補充意見,從而和我們?nèi)〉寐?lián)系。

作者:Aaron Kili [6] 譯者:ucasFL校對:wxy

本文由 LCTT[7] 原創(chuàng)編譯,Linux中國榮譽推出

[1]: http://www./different-types-of-linux-shells/

[2]: http://www./pwd-command-examples/

[3]: http://www./cd-command-in-linux/

[4]: http://www./history-command-examples/

[5]: http://www./different-types-of-linux-shells/

[6]: http://www./author/aaronkili/

[7]: https://github.com/LCTT/TranslateProject

推薦文章

將文章分享給朋友是對我們最好的贊賞!

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多