Linux基礎系列-Day8

Shell編程基礎linux

Shell介紹nginx

Shell俗稱殼(用來區別於核),是指「提供使用者使用界面」的軟件(命令解析器)。它相似於windows下的的cmd.exe。它接收用戶命令,而後調用相應的應用程序,shell可使命令行也就能夠是圖形界面,用來提供人機交互。正則表達式

 

通配符shell

通配符能夠理解爲shell的特殊代號字符,通配符就是一類特殊符號的集合,在shell解釋器中有特殊的含義。編程

通配符字符含義說明:windows

  ~   表示用戶家目錄緩存

[root@linux-test etc]# pwd
/etc
[root@linux-test etc]# cd ~
[root@linux-test ~]# pwd
/root

  ``和$()  被``和()裹起來的命令先執行bash

[root@linux-test ~]# echo `ls`
anaconda-ks.cfg Desktop Documents Downloads initial-setup-ks.cfg Music Pictures Public Templates Videos
[root@linux-test ~]# echo $(ls)
anaconda-ks.cfg Desktop Documents Downloads initial-setup-ks.cfg Music Pictures Public Templates Videos
[root@linux-test ~]# echo ls
ls

  !歷史  能夠調用歷史命令,也表示邏輯非(下面^部分說明)ide

[root@linux-test test]# history 
    1  pwd
    2  history 
[root@linux-test test]# !1      匹配歷史命令編號
pwd
/root/test
[root@linux-test test]# !pwd    匹配最近一條歷史
pwd
/root/test

  *   表示任意多個字符函數

[root@linux-test ~]# ls
anaconda-ks.cfg  Desktop  Documents  Downloads  initial-setup-ks.cfg  Music   Pictures  Public  Templates  Videos
[root@linux-test ~]# ls *.cfg              #查看以.cfg結尾文件和目錄,以前字符任意全匹配
anaconda-ks.cfg  initial-setup-ks.cfg

  ?   表示一個字符,能夠多個?同時用

[root@linux-test test]# ls
123.txt  12.txt  1.txt  45ab.txt
[root@linux-test test]# ls ?.txt
1.txt
[root@linux-test test]# ls ??.txt
12.txt

  []   表明一個和,如[0-9]表示0-9十個數字,[1,3]表示1和3

[root@linux-test test]# ls
123.txt  12.txt  16.txt  1.txt  45ab.txt
[root@linux-test test]# ls 1[0-9].txt
12.txt  16.txt

  ^   表示取反,這個通配符必需要在[]中使用,以下顯示不是以cfg結尾的文件和目錄,^能夠用!取代

[root@linux-test ~]# ls *[^cfg]
Desktop:
Documents:
Downloads:
Pictures:
Templates:
Videos:

  @   無心義,通常用來隔離字符串,通常用於文件內容中

  #   用於註釋字符串,通常用於配置文件中的註釋

  $   取值,通常用於變量取值

[root@linux-test test]# x=5
[root@linux-test test]# echo x
x
[root@linux-test test]# echo $x
5

  %   用來殺死一個進程

  &  後臺執行命令

[root@linux-test test]# touch aaa.txt &
[1] 46465    &掛後臺執行會反饋一個進程號

  &&  邏輯與,當前一個指令執行成功時,執行後一個指令

[root@linux-test test]# ls && pwd
123.txt  12.txt  16.txt  1.txt  45ab.txt  aaa.txt
/root/test

  ||  邏輯或,當前一個指令執行失敗時,執行後一個指令

[root@linux-test test]# lp || pwd
lp: Error - no default destination available.
/root/test
[root@linux-test test]# ls || pwd
123.txt  12.txt  16.txt  1.txt  45ab.txt  aaa.txt

  ()  子進程運行命令

[root@linux-test test]# (y=6)
[root@linux-test test]# echo $y      #因爲不是在當前進程中執行,因此沒法查看變量

  +-*/=  運算符,多用於循環和判斷語句中

  ''  硬引用,裏面的字符無心義

  ""  軟引用,具備變量置換功能

[root@linux-test test]# z=6
[root@linux-test test]# echo 'Num = $z'
Num = $z
[root@linux-test test]# echo "Num = $z"
Num = 6

  :  執行後永遠爲真

  echo $?  上一次執行命令的執行狀態,0表示成功

[root@linux-test test]# ls
123.txt  12.txt  16.txt  1.txt  45ab.txt  aaa.txt
[root@linux-test test]# echo $?
0
[root@linux-test test]# aaaa
bash: aaaa: command not found...
[root@linux-test test]# echo $?
127
[root@linux-test test]# :
[root@linux-test test]# echo $?
0

  {}  多個文件集合

[root@linux-test test]# ls
123.txt  12.txt  16.txt  1.txt  45ab.txt  aaa.txt
[root@linux-test test]# ls {123.txt,12.txt}
123.txt  12.txt

  /    路徑分隔符號

  >和>>     輸出重定向導向,分別爲"覆蓋"與"累加" 

 

變量

變量是用來表示程序運行時候改變的狀態的,是一個抽象的概念。

變量組成:

  [變量名]=[變量實際的值]

  變量名:命名通常字母或下劃線開頭,剩下的能夠是字母和數字,變量名不能和關鍵字衝突,如ls=1

  變量值:能夠改變的值,一個變量名能夠重複賦值,可是變量實際的值爲最後賦值後的值

變量類型:

  局部變量:只在某一段代碼中生效的變量

  全局變量:在整個程序的運行中生效的變量

[root@linux-test ~]# n=1          #當前bash進程中設置局部變量
[root@linux-test ~]# bash          #切換bash到子進程
[root@linux-test ~]# echo $n        #查看變量爲空

[root@linux-test ~]# export x=2      #當前bash進程中設置全局變量
[root@linux-test ~]# bash          #切換bash到子進程
[root@linux-test ~]# echo $x        #查看變量
2

Shell命令執行優先級

==> alias        別名
  ==> Compound Commands    複合命令(if、for、while等)
    ==> function               函數
      ==> build_in                內置命令(cd等)
        ==> hash                    hash命令,緩存中用過的命令
          ==> $PATH                    環境變量標明的命令
            ==> error: command not found              找不到命令時候

bash環境

如下四個文件在登陸用戶的過程當中會依次執行

  /etc/profile

  ~/.bash_profile

  ~/.bashrc

  /etc/bashrc

su user不加-登陸,只會加載~/.bashrc、/etc/bashrc

全局環境變量配置通常放在/etc/profile文件中,用戶級環境變量通常放在~/.bash_profile

臨時設置環境變量:

[root@linux-test ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@linux-test ~]# PATH=/usr/local/nginx/sbin/:$PATH
[root@linux-test ~]# echo $PATH
/usr/local/nginx/sbin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

環境變量中查找命令的順序是依次日後,好比ls命令在當前設置的變量中,首先會從/usr/local/nginx/sbin下找,找不到會從/usr/local/sbin下找,依次找到爲止。

永久設置環境變量:

[root@linux-test ~]# echo "export PATH=/usr/local/nginx/sbin:$PATH" >> /etc/profile    #將環境變量的修改寫入到開啓加載的腳本文件中便可

grep命令詳細使用說明

  -v  反轉匹配

  -i  忽略大小寫匹配

  -r  遞歸匹配

  -w  單詞匹配,屬徹底匹配,只要有分隔符分割便可

  -n  顯示行號

  -q  靜默模式,沒有任何輸出,得用$?來判斷執行成功沒有,即有沒有過濾到想要的內容

  -A  若是匹配成功,則將匹配行及其後n行一塊兒打印出來

  -B  若是匹配成功,則將匹配行及其前n行一塊兒打印出來

  -C  若是匹配成功,則將匹配行及其先後n行一塊兒打印出來

  -c  匹配成功,只打印匹配的行數(一共匹配到多少行的數量)打印出來

  -E  等於egrep,擴展,多用於加入正則表達式後的匹配

  -l  列出文件內容符合指定的範本樣式的文件名稱,多和-r聯用

  -L  列出文件內容不符合指定的範本樣式的文件名稱,多和-r聯用

  -s  不顯示錯誤信息

  -o  只輸出文件中匹配到的部分

正則表達式

正則表達式和通配符同樣,也是一組特殊符號,通配符是由shell解釋執行,正則表達式是由命令解釋。

相關文章
相關標籤/搜索