當你打算真正操縱好你的 Linux 系統,沒有什么能比命令行界面更讓你做到這一點。為了成為一個 Linux 高手,你必須能夠理解 Shell命令的不同類型,並且會在終端下正確的使用它們。
在 Linux 下,命令有幾種類型,對於一個 Linux 新手來說,知道不同命令的意思才能夠高效和准確的使用它們。因此,在這篇文章里,我們將會遍及各種不同分類的 Linux Shell 命令。需要注意一件非常重要的事:命令行界面和 Shell 是不同的,命令行界面只是為你提供一個訪問 Shell 的方式。而 Shell ,它是可編程的,這使得它可以通過命令與內核進行交流。下面列出了 Linux 下命令的不同種類:
當你執行一條命令的時候,Linux 通過從左到右搜索存儲在 $PATH 環境變量中的目錄來找到這條命令的可執行文件。你可以像下面這樣查看存儲在 $PATH 中的目錄:
$ echo $PATH /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
然后一直接着下去。在搜索過程中,搜索順序是至關重要的。比如在 /usr/bin 目錄里的文件系統中的命令 $ ll /bin/。
示例輸出:
total 16284 drwxr-xr-x 2 root root 4096 Jul 31 16:30 ./ drwxr-xr-x 23 root root 4096 Jul 31 16:29 ../ -rwxr-xr-x 1 root root 6456 Apr 14 18:53 archdetect* -rwxr-xr-x 1 root root 1037440 May 17 16:15 bash* -rwxr-xr-x 1 root root 520992 Jan 20 2016 btrfs* -rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfs-calc-size* lrwxrwxrwx 1 root root 5 Jul 31 16:19 btrfsck -> btrfs* -rwxr-xr-x 1 root root 278376 Jan 20 2016 btrfs-convert* -rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfs-debug-tree* -rwxr-xr-x 1 root root 245368 Jan 20 2016 btrfs-find-root* -rwxr-xr-x 1 root root 270136 Jan 20 2016 btrfs-image* -rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfs-map-logical* -rwxr-xr-x 1 root root 245368 Jan 20 2016 btrfs-select-super* -rwxr-xr-x 1 root root 253816 Jan 20 2016 btrfs-show-super* -rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfstune* -rwxr-xr-x 1 root root 245368 Jan 20 2016 btrfs-zero-log* -rwxr-xr-x 1 root root 31288 May 20 2015 bunzip2* -rwxr-xr-x 1 root root 1964536 Aug 19 2015 busybox* -rwxr-xr-x 1 root root 31288 May 20 2015 bzcat* lrwxrwxrwx 1 root root 6 Jul 31 16:19 bzcmp -> bzdiff* -rwxr-xr-x 1 root root 2140 May 20 2015 bzdiff* lrwxrwxrwx 1 root root 6 Jul 31 16:19 bzegrep -> bzgrep* -rwxr-xr-x 1 root root 4877 May 20 2015 bzexe* lrwxrwxrwx 1 root root 6 Jul 31 16:19 bzfgrep -> bzgrep* -rwxr-xr-x 1 root root 3642 May 20 2015 bzgrep*
這些是用戶定義的命令,它們是通過 shell 內置命令 alias 創建的,其中包含其它一些帶有選項和參數的 shell 命令。這個意圖主要是使用新穎、簡短的名字來替代冗長的命令。創建一個別名的語法像下面這樣:
$ alias newcommand='command -options'
通過下面的命令,可以列舉系統中的所有別名:
$ alias -p 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$//'/'')"' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias l='ls -CF' alias la='ls -A' alias ll='ls -alF' alias ls='ls --color=auto'
要在 Linux 中創建一個新的別名,仔細閱讀下面的例子。
$ alias update='sudo apt update' $ alias upgrade='sudo apt dist-upgrade' $ alias -p | grep 'up'
然而,上面這些我們創建的別名只能暫時的工作,當經過下一次系統啟動后它們不再工作。你可以像下面展示的這樣在 '.bashrc' 文件中設置永久別名。
添加以后,運行下面的命令來激活:
$ source ~/.bashrc
在 shell 程序設計中,
if、then、fi、for、while、case、esac、else、until
以及其他更多的字都是 shell 保留字。正如描述所暗示的,它們在 shell 中有特殊的含義。你可以通過使用下面展示的 type 命令來列出所有的 shell 關鍵字:
$ type if then fi for while case esac else until if is a shell keyword then is a shell keyword fi is a shell keyword for is a shell keyword while is a shell keyword case is a shell keyword esac is a shell keyword else is a shell keyword until is a shell keyword
一個 shell 函數是一組在當前 shell 內一起執行的命令。函數有利於在 shell 腳本中實現特殊任務。在 shell 腳本中寫 shell 函數的傳統形式是下面這樣:
function_name() { command1 command2 ...... }
或者像這樣:
function function_name { command1 command2 ...... }
讓我們看一看如何在一個名為 shell_functions.sh 的腳本中寫 shell 函數。
#!/bin/bash #write a shell function to update and upgrade installed packages upgrade_system(){ sudo apt update; sudo apt dist-upgrade; } #execute function upgrade_system
取代通過命令行執行兩條命令:
sudo apt update
和
sudo apt dist-upgrade
我們在腳本內寫了一個像執行一條單一命令一樣來執行兩條命令的 shell 函數 upgrade_system。保存文件,然后使腳本可執行。最后像下面這樣運行 shell 函數:
$ chmod +x shell_functions.sh $ ./shell_functions.sh
這些是在 shell 中內置的 Linux 命令,所以你無法在文件系統中找到它們。這些命令包括 pwd、cd、bg、alias、history、type、source、read、exit 等,你可以通過下面展示的 type 命令來列出或檢查 Linux 內置命令:
$ type pwd pwd is a shell builtin $ type cd cd is a shell builtin $ type bg bg is a shell builtin $ type alias alias is a shell builtin $ type history history is a shell builtin
作為一個 Linux 用戶,知道你所運行的命令類型是很重要的。我相信,通過上面明確、簡單並且易於理解的解釋,包括一些相關的說明,你可能對 “Linux命令的不同種類”有了很好的理解。
本文轉載地址:https://www.linuxprobe.com/linux-shell.html