在計算機科學中,Shell俗稱殼(用來區別於核),Shell 是一個 C 語言編寫的腳本語言,它是用戶與 Linux 的橋樑,用戶輸入命令交給 Shell 處理, Shell 將相應的操做傳遞給內核(Kernel),內核把處理的結果輸出給用戶。簡單來講就是一我的機交互的中間處理器。前端
下面是流程示意圖:python
GUI 爲 Unix 或者類 Unix 操做系統構造一個功能完善、操做簡單以及界面友好的桌面環境。例如:應用最爲普遍的是微軟的windows系列操做系統,還有也包括廣爲人知的 Linux shell,其中linux shell ,以及功能更強大的CDE、GNOME、KDE、 XFCE。linux
CLI 是在用戶提示符下鍵入可執行指令的界面,用戶經過鍵盤輸入指令,完成一系列操做。 在 Linux 系統上主流的 CLI 實現是 Bash,是許多 Linux 發行版默認的 Shell。還有許多 Unix 上 Shell,例如 tcsh、csh、ash、bsh、ksh 等。正則表達式
Shell 是一個程序,通常都是放在/bin或者/user/bin目錄下,當前 Linux 系統可用的 Shell 都記錄在/etc/shells文件中。/etc/shells是一個純文本文件,你能夠在圖形界面下打開它,也可使用 cat 命令查看它。shell
經過 cat 命令來查看當前 Linux 系統的可用 Shell:數據庫
cat /etc/shells /bin/sh /bin/bash /sbin/nologin /usr/bin/sh /usr/bin/bash /usr/sbin/nologin /bin/tcsh /bin/csh
在現代的 Linux 上,sh 已經被 bash 代替,/bin/sh每每是指向/bin/bash的符號連接。 編程
若是你但願查看當前 Linux 的默認 Shell,那麼能夠輸出 SHELL 環境變量:vim
echo $SHELL
/bin/bash
出結果代表默認的 Shell 是 bash。 windows
SHELL是 Linux 系統中的環境變量,它指明瞭當前使用的 Shell 程序的位置,也就是使用的哪一個 Shell。後端
麼是腳本?
腳本簡單地說就是一條條的文字命令(一些指令的堆積),這些文字命令是能夠看到的(如能夠用記事本打開查看、編輯)。
常見的腳本: JavaScript(JS,前端),VBScript, ASP,JSP,PHP(後端),SQL(數據庫操做語言),Perl,Shell,python,Ruby,JavaFX, Lua等。
爲何要學習和使用shell?
Shell屬於內置的腳本
程序開發的效率很是高,依賴於功能強大的命令能夠迅速地完成開發任務(批處理)
語法簡單,代碼寫起來比較輕鬆,簡單易學
新建一個一 .sh 結尾的文件打開
編寫
# vim test.sh #!/bin/bash echo "Hello world!"
第一行指定解釋器,第二行打印 Hello world!
寫好後,開始執行,執行 Shell 腳本有三種方法:
也是最經常使用的一種
# bash test.sh Hello world!
# ll test.sh -rw-r--r--. 1 root root 32 Aug 18 01:07 test.sh # chmod +x test.sh # ./test.sh -bash: ./test.sh: Permission denied # chmod +x test.sh # ./test.sh Hello world!
這種方式默認根據腳本第一行指定的解釋器處理,若是沒寫以當前默認 Shell 解釋器執行。
注意,這裏在運行時必定要寫成 ./test.sh(絕對路徑亦可),而不是 test.sh,運行其它二進制的程序也同樣,直接寫 test.sh,Linux 系統會去 PATH(環境變量) 裏尋找有沒有叫 test.sh 的,而只有 /bin, /sbin, /usr/bin,/usr/sbin 等在 PATH 裏,你的當前目錄一般不在 PATH 裏,因此寫成 test.sh 是會找不到命令的,要用 ./test.sh 告訴系統說,就在當前目錄找。
# source test.sh
Hello world!
咱們可使用命令pstree查看咱們當前所處的位置
須要下載
[root@ken ~]# yum search pstree Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile =================== Matched: pstree ======================= psmisc.x86_64 : Utilities for managing processes on your system texlive-pst-tree.noarch : Trees, using pstricks [root@ken ~]# yum install psmisc -y
使用pstree
咱們再次執行bash,就會進入到另一個子shell中
這個時候咱們在這個子shell中定義一個變量,發現能夠正確打印出來
[root@ken ~]# age=25 [root@ken ~]# echo $age 25
如今咱們退出當前的shell,即進入了當前子shell中的父shell中,再次打印咱們剛纔定義的變量
能夠發現如今已經沒法獲取到咱們剛纔定義的變量值了。
咱們把環境變量定義到profile的一個子文件中,並使用source執行該文件並生效
打開一個子shell,定義在父shell中的環境變量依然有效
[root@ken ~]# vim /etc/profile.d/ken.sh export name=ken #寫入全局變量 [root@ken ~]# source /etc/profile.d/ken.sh [root@ken ~]# echo $name ken [root@ken ~]# bash [root@ken ~]# echo $name ken
環境變量可用於定義shell的運行環境,環境變量能夠在配置文件中定義與修改,也能夠在命令行中設置,可是命令行中的修改操做在終端重啓時就會丟失,所以最好在配置文件中修改——全局配置 「/etc/profile「 或在它的子配置文件 「/etc/profile.d」 中定義。(推薦在子配置文件中進行配置不會對系統產生較大影響)將環境變量放在profile文件中,每次用戶登陸時這些變量值將被初始化。好比HOME、USER、SHELL、UID等再用戶登陸以前就已經被/bin/login程序設置好了。
本地變量在用戶當前的shell生存期的腳本中使用。在一個函數中將某個變量聲明爲local,則該變量就是一個局部變量,只在本函數中有效。
環境變量能夠在定義它們的shell及其派生出來的任意子進程的shell中使用。局部變量只能在定義它們的函數/腳本中使用。子shell和父shell互不繼承局部變量,但子shell繼承父shell的全局變量
其實shell就是常見命令寫在一塊兒就變成了了腳本文件。這裏介紹幾個在shell裏經常使用的命令
過濾來自一個文件或標準輸入匹配模式內容。
除了 grep 外,還有 egrep。egrep 是 grep 的擴展,至關於 grep -E
grep <options> <參數> ...... <file>
選項 | 描述 |
-E,--extended-regexp |
模式是擴展正則表達式(ERE) |
-i,--ignore-case | 忽略大小寫 |
-n,--line-number | 打印行號 |
-o,--only-matching | 只打印匹配的內容 |
-c,--count | 只打印每一個文件匹配的行數 |
-B,--before-context=NUM | 打印匹配的前幾行 |
-A,--after-context=NUM | 打印匹配的後幾行 |
-C,--context=NUM |
打印匹配的先後幾行 |
--color[=WHEN], |
匹配的字體顏色 |
-v,--invert-match |
打印不匹配的行 |
例如
[root@ken ~]# echo "this is ken THIS IS KEN" | grep -i 'ken' this is ken THIS IS KEN [root@ken ~]# echo "this is ken THIS IS KEN" | grep 'ken' this is ken THIS IS KEN
[root@ken ~]# grep -n 'root' /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 10:operator:x:11:0:operator:/root:/sbin/nologin
[root@ken ~]# echo "this is ken THIS IS KEN" | grep -o 'ken' ken [root@ken ~]# echo "this is ken THIS IS KEN" | grep 'ken' this is ken THIS IS KEN
[root@localhost /]# cat 1.txt 1111111 1111111 2222222 2222222 1111111 2222222 3333333 1111111 [root@localhost /]# grep "11" -c 1.txt 4 [root@localhost /]# grep "2" -c 1.txt 3 [root@localhost /]#
[root@localhost /]# cat 1.txt 000 111 222 333 444 555 666 [root@localhost /]# grep -B 2 "3" 1.txt 111 222 333
[root@localhost /]# cat 1.txt 000 111 222 333 444 555 666 [root@localhost /]# grep -A 2 "3" 1.txt 333 444 555
[root@localhost /]# cat 1.txt 000 111 222 333 444 555 666 [root@localhost /]# grep -C 2 "3" 1.txt 111 222 333 444 555
[root@localhost /]# cat 1.txt 000 111 222 333 444 555 666 [root@localhost /]# grep -v 111 1.txt 000 222 333 444 555 666 [root@localhost /]#
cut [options] [file]
cut 命令從文件的每一行剪切字節、字符和字段並將這些字節、字符和字段寫至標準輸出。
若是不指定 File 參數,cut 命令將讀取標準輸入。必須指定 -b、-c 或 -f 標誌之一。
經常使用參數:
-c :以字符爲單位進行分割。
-d :自定義分隔符,但只能爲一個字符長
-f :與-d一塊兒使用,指定顯示哪一個區域。
一、-c
[root@ken ~]# echo "this is ken" | cut -c 4 s [root@ken ~]# echo "this is ken" | cut -c 5 [root@ken ~]# echo "this is ken" | cut -c 2 h [root@ken ~]# echo "this is ken" | cut -c 1-5 this
二、-d ,-f 一般連用
從標準中讀取
[root@songqi ~]# echo 'this is ken' | cut -d ' ' -f 3 ken [root@songqi ~]# echo "name:ken age:25 gender:male" | cut -d ' ' -f2 age:25
從文件中讀取
[root@ken ~]# echo "this is ken">test [root@ken ~]# cut -d' ' -f3 test ken
Linux sort命令用於將文本文件內容加以排序。
sort可針對文本文件的內容,以行爲單位來排序。
sort [-bcdfimMnr][-o<輸出文件>][-t<分隔字符>][+<起始欄位>-<結束欄位>][--help][--verison][文件]
在使用sort命令以默認的式對文件的行進行排序,使用的命令以下:
[root@ken ~]# cat test 6 this is ken 2 this is ken 5 this is ken 6 this is ken 1 this is ken 8 this is ken [root@ken ~]# sort test 1 this is ken 2 this is ken 5 this is ken 6 this is ken 6 this is ken 8 this is ken
sort 命令將以默認的方式將文本文件的第一列以ASCII 碼的次序排列,並將結果輸出到標準輸出。
[root@ken ~]# sort -r test | head -3 8 this is ken 6 this is ken 6 this is ken
[root@ken ~]# sort -t 'e' -k 2 test 6 this:is:ke1 5 this:is:ke2 6 this:is:ke3 2 this:is:ke4 1 this:is:ke6 8 this:is:ke7 [root@ken ~]# sort -t 'e' -k 2 -r test 8 this:is:ke7 1 this:is:ke6 2 this:is:ke4 6 this:is:ke3 5 this:is:ke2 6 this:is:ke1
[root@ken ~]# sort test 1 this:is:ke6 2 this:is:ke4 2 this:is:ke4 2 this:is:ke4 2 this:is:ke4 5 this:is:ke2 6 this:is:ke1 6 this:is:ke3 8 this:is:ke7 [root@ken ~]# sort -u test 1 this:is:ke6 2 this:is:ke4 5 this:is:ke2 6 this:is:ke1 6 this:is:ke3 8 this:is:ke7
去除重複的行(相鄰且相同,認定爲重複)
-c:在行首用數字表示該行出現了多少次
-u:僅僅顯示那些沒有出現重複過的行
[root@ken ~]# uniq -c test 1 6 this:is:ke3 4 2 this:is:ke4 1 5 this:is:ke2 1 6 this:is:ke1 1 1 this:is:ke6 1 8 this:is:ke7
[root@ken ~]# sort test | uniq 1 this:is:ke6 2 this:is:ke4 5 this:is:ke2 6 this:is:ke1 6 this:is:ke3 8 this:is:ke7
Linux tr 命令用於轉換或刪除文件中的字符。
tr 指令從標準輸入設備讀取數據,通過字符串轉譯後,將結果輸出到標準輸出設備。
a-z 任意小寫
A-Z 任意大寫
0-9 任意數字
[root@ken ~]# echo "this is ken" | tr a-z A-Z THIS IS KEN [root@ken ~]# echo "THIS IS KEN" | tr A-Z a-z this is ken
s這個字符串都會被刪掉
[root@ken ~]# cat test 6 this:is:ke3 2 this:is:ke4 2 this:is:ke4 2 this:is:ke4 2 this:is:ke4 5 this:is:ke2 6 this:is:ke1 1 this:is:ke6 8 this:is:ke7 [root@ken ~]# cat test | tr -d 's' 6 thi:i:ke3 2 thi:i:ke4 2 thi:i:ke4 2 thi:i:ke4 2 thi:i:ke4 5 thi:i:ke2 6 thi:i:ke1 1 thi:i:ke6 8 thi:i:ke7