Bash編程基礎知識學習第一節

來源:互聯網
上載者:User

標籤:程式   基礎知識   作業系統   最大限度   

Bash編程基礎知識學習

一、bash的由來

1 、什麼是Bash 什麼是Shell Bash 是GNU Bourne-Again SHell,是目前大多數Linux發行版採用的Shell。

Shell 是*nix提供給使用者使用的介面。一個作業系統的底層是獨立啟動並執行,

使用者介面和底層分開,可以最大限度的保證系統穩定。Shell就是一個特殊的程式,負責接受使用者的命令,並把系統的響應返回給使用者。

Bash Shell

一般不需要自己安裝,在安裝發行版的同時都會安裝好。

Shell的存在是和登陸相關的,從使用者名稱密碼驗證通過以後,Shell就啟動了,知道你退出,Shell就結

束了運行。除了Bash Shell之外,還有很多其他的Shell

650) this.width=650;" style="background-image:none;border:0px;padding-left:0px;padding-right:0px;padding-top:0px;width:400px;height:254px;" title="wps_clip_image-25544" border="0" alt="wps_clip_image-25544" src="http://img1.51cto.com/attachment/201408/3/8400375_1407046197UG1D.png" height="254" width="400" hspace="0" vspace="0" />

你可在任何程式設計語言中使用變數,但是在指令碼編程中它們是沒有類型的,簡稱弱類型程式設計語言,在這個

變數中可以保含一個數字,一個字串,一個單詞等。你並不需要聲明這個變數,它會在引用這個變數

時建立它。 
使用變數來實現一個簡單的Hello World 
#!/bin/bash 
STR="Hello World!" 
echo $STR

二、bash特性之檔案名稱通配(globbing):

650) this.width=650;" style="background-image:none;border:0px;padding-left:0px;padding-right:0px;padding-top:0px;width:400px;height:208px;" title="wps_clip_image-6418" border="0" alt="wps_clip_image-6418" src="http://img1.51cto.com/attachment/201408/3/8400375_1407046203jp1D.png" height="208" width="400" hspace="0" vspace="0" />

*: 任意長度的任一字元p*d, pad, pbd, pd*ab*c?: 匹配任意單字元[]: 匹配指定範圍內的任意單字元[abc], [a-z], [0-9], [0-9a-z][^]:匹配指定範圍以外的任意單字元[^0-9a-z]字元集合:[:space:] : 所有空白字元[:punct:] : 所有標點符號[:lower:] :所有小寫字母[:upper:] :所有大寫字母[:digit:]  :數字[:alnum:] :數字和字母[:alpha:] : 所有字母

三、輸入輸出

>>  /dev/null      cat  /path/file  <<  /somewhere/file

練習:寫一指令碼

1、添加10個使用者:tuser601-tuser610

如果使用者不存在,才添加,並以綠色顯示添加成功;如果存在,則以紅色顯示已經有此使用者;

2、顯示一共添加了多少個使用者;

#!/bin/bash#declare -i count=0for i in {501..510}; doif id tuser$i &> /dev/null; thenecho -e "\033[31mtuser$i\033[0m exists."elseuseradd tuser$iecho -e "add user \033[32mtuser$i\033[0m successfully."let count++  fidoneecho "Total add $count users."

檔案:檔案系統(核心)

標準輸入:0鍵盤標準輸出:1監視器錯誤輸出:2監視器重新導向意味著:改變其標準位置輸出重新導向:COMMAND > POSITION:覆蓋輸出COMMAND >> POSITION: 追加輸出錯誤重新導向:COMMAND 2> POSITION:覆蓋輸出COMMAND 2>> POSITION: 追加輸出合并重新導向:COMMAND &> POSITIONCOMMAND > POSITION 2> &1bash的重要特性:變數

四、bash變數類別:

本地變數:只對當前shell進程有效變數;對其它shell進程無效,包當前shell進程的子進程;

VAR_NAME=VALUE

變數賦值:向變數的儲存空間儲存資料

變數引用:${VAR_NAME}

650) this.width=650;" style="background-image:none;border:0px;padding-left:0px;padding-right:0px;padding-top:0px;width:400px;height:240px;" title="wps_clip_image-9798" border="0" alt="wps_clip_image-9798" src="http://img1.51cto.com/attachment/201408/3/8400375_14070462124aVb.png" height="240" width="400" hspace="0" vspace="0" />

"":弱引用,裡面的變數會被替換;

‘‘:強引用,裡面的所有字元都是字面量,直接輸出;

650) this.width=650;" style="background-image:none;border:0px;padding-left:0px;padding-right:0px;padding-top:0px;width:400px;height:256px;" title="wps_clip_image-8925" border="0" alt="wps_clip_image-8925" src="http://img1.51cto.com/attachment/201408/3/8400375_1407046220QSoW.png" height="256" width="400" hspace="0" vspace="0" />

環境變數:對當前shell進程及其子shell有效,對其它的shell進程無效;定義:export VAR_NAME=VALUE匯出:export VAR_NAMEExport LANG=en 修改字型,只對當前進程有效使用者可自義環境變數bash有許多內建的環境變數撤消變數:unset VAR_NAME唯讀變數:readonly VAR_NAME局部變數:  local

650) this.width=650;" style="background-image:none;border:0px;padding-left:0px;padding-right:0px;padding-top:0px;width:400px;height:241px;" title="wps_clip_image-32302" border="0" alt="wps_clip_image-32302" src="http://img1.51cto.com/attachment/201408/3/8400375_1407046223HpZb.png" height="241" width="400" hspace="0" vspace="0" />

對shell指令碼中某代碼片斷有效;通常用於函數本地;local VAR_NAME=VALUE位置變數:$1, $2, ..., ${10}特殊變數:$?查看當前shell進程中的所有變數:set查看當前shell進程中的所有環境變數:export, printenv, env登入類型:五、指令碼在電腦中執行次序互動式:直接通過終端輸入帳號和密碼登入;使用su -l USERNAME 或 su - USERNAME;非互動式:su USERNAME圖形介面下開啟的終端執行指令碼通過編輯設定檔修改的配置生效?1、退出並重新登入;2、讓bash重讀此設定檔;. FILEsource FILE互動登入的使用者:/etc/profile --> /etc/profile.d/*.sh --> ~/.bash_profile --> ~/.bashrc --> /etc/bashrc非互動登入的使用者:~/.bashrc --> /etc/bashrc --> /etc/profile.d/*.sh文法高亮::syntax on|off搜尋高亮::set hlsearch:set nohlsearch設定檔:全域:/etc/vimrc使用者:~/.vimrc

六、shell指令碼格式:

第一行,頂格:shebang#!/bin/bash#!/usr/bin/python其它的以#開頭的行均為注釋,會被解譯器忽略

練習:寫一個指令碼

1、指令碼可以接受一個以上的檔案路徑作為參數;

2、顯示每個檔案所擁的行數;

3、顯示本次共對多少個檔案執行了行數統計;

指令碼

650) this.width=650;" style="background-image:none;border:0px;padding-left:0px;padding-right:0px;padding-top:0px;width:400px;height:184px;" title="wps_clip_image-32750" border="0" alt="wps_clip_image-32750" src="http://img1.51cto.com/attachment/201408/3/8400375_1407046228xegG.png" height="184" width="400" hspace="0" vspace="0" />

執行結果

650) this.width=650;" style="background-image:none;border:0px;padding-left:0px;padding-right:0px;padding-top:0px;width:400px;height:202px;" title="wps_clip_image-27338" border="0" alt="wps_clip_image-27338" src="http://img1.51cto.com/attachment/201408/3/8400375_1407046239pI9t.png" height="202" width="400" hspace="0" vspace="0" />

練習:寫一個指令碼

1、顯示/etc/passwd檔案中位於檔案的第偶數行的使用者名稱;並顯示共有多少個這樣的使用者;

指令碼

650) this.width=650;" style="background-image:none;border:0px;padding-left:0px;padding-right:0px;padding-top:0px;width:400px;height:179px;" title="wps_clip_image-6223" border="0" alt="wps_clip_image-6223" src="http://img1.51cto.com/attachment/201408/3/8400375_1407046249Lit5.png" height="179" width="400" hspace="0" vspace="0" />

結果

650) this.width=650;" style="background-image:none;border:0px;padding-left:0px;padding-right:0px;padding-top:0px;width:400px;height:153px;" title="wps_clip_image-18979" border="0" alt="wps_clip_image-18979" src="http://img1.51cto.com/attachment/201408/3/8400375_1407046253o8zR.png" height="153" width="400" hspace="0" vspace="0" />

七、bash當中的關鍵詞法基礎

1、數值測試條件符和字串操作符

650) this.width=650;" style="background-image:none;border:0px;padding-left:0px;padding-right:0px;padding-top:0px;width:400px;height:192px;" title="wps_clip_image-14189" border="0" alt="wps_clip_image-14189" src="http://img1.51cto.com/attachment/201408/3/8400375_1407046256RTxr.png" height="192" width="400" hspace="0" vspace="0" />

2、字串測試

650) this.width=650;" style="background-image:none;border:0px;padding-left:0px;padding-right:0px;padding-top:0px;width:400px;height:225px;" title="wps_clip_image-4463" border="0" alt="wps_clip_image-4463" src="http://img1.51cto.com/attachment/201408/3/8400375_14070462639WnQ.png" height="225" width="400" hspace="0" vspace="0" />

3、邏輯與或運算

-a 與  -o 或  ! 非  、   &&   且   ||   且  前面執行完了,執行後面的,前面錯誤後面就不執行啦。組合條件測試:在多個條件間實現邏輯運算與:[ condition1 -a condition2 ]condition1 && condition2或:[ condition1 -o condition2 ]condition1 || condition2非:[ -not condition ]! condition

650) this.width=650;" style="background-image:none;border:0px;padding-left:0px;padding-right:0px;padding-top:0px;width:400px;height:272px;" title="wps_clip_image-24372" border="0" alt="wps_clip_image-24372" src="http://img1.51cto.com/attachment/201408/3/8400375_1407046271sROe.png" height="272" width="400" hspace="0" vspace="0" />

650) this.width=650;" style="background-image:none;border:0px;padding-left:0px;padding-right:0px;padding-top:0px;width:400px;height:209px;" title="wps_clip_image-24117" border="0" alt="wps_clip_image-24117" src="http://img1.51cto.com/attachment/201408/3/8400375_1407046276mo4v.png" height="209" width="400" hspace="0" vspace="0" />

練習:提示使用者輸入一個使用者名稱,如果使用者存在,就顯示使用者的ID號和shell;否則顯示使用者不

存在;  顯示完成之後不退出,再次重複前面的操作,直到使用者輸入q或quit為止;

 

read -p "Plz enter a username: " userName  while [ "$userName" != ‘q‘ -a "$userName" != ‘quit‘ ]; do  if id $userName &> /dev/null; then      grep "^$userName\>" /etc/passwd | cut -d: -f3,7  else  echo "No such user."  fi  read -p "Plz enter a username again: " userName  Done

練習:求100以內所有偶數之和;要求使用模數方法;

#!/bin/bashdeclare -i counter=1declare -i sum=0while [ $counter -le 100 ]; do    if [ $[$counter%2] -eq 0 ]; then        let sum+=$counter    fi    let counter++doneecho $sum

2、檔案測試:

-e 檔案名稱:如果檔案存在則為真-r 檔案名稱:如果檔案存在且可讀則為真-w 檔案名稱:如果檔案存在且可寫則為真-x 檔案名稱:如果檔案存在且可執行則為真-s 檔案名稱:如果檔案存在且至少有一個字元則為真-d 檔案名稱:如果檔案存在且為目錄則為真-f 檔案名稱:如果檔案存在且為普通檔案則為真-c 檔案名稱:如果檔案存在且為字元型特殊檔案則為真-b 檔案名稱:如果檔案存在且為塊特殊檔案則為真

650) this.width=650;" style="background-image:none;border:0px;padding-left:0px;padding-right:0px;padding-top:0px;width:400px;height:244px;" title="wps_clip_image-30006" border="0" alt="wps_clip_image-30006" src="http://img1.51cto.com/attachment/201408/3/8400375_1407046287NcER.png" height="244" width="400" hspace="0" vspace="0" />

練習:寫一個指令碼,完成如下任務:

1、分別複製/var/log下的檔案至/tmp/logs/目錄中;2、複製目錄時,才使用cp -r3、複製檔案時,使用cp4、複製連結檔案,使用cp -d5、餘下的類型,使用cp -aif [ -d $1 ];thencp -r /var/log/$1 /tmp/logselif [ -d $1 ];thencp -r /var/log/$1 /tmp/logsif [ -f $1 ];thencp  /var/log/$1 /tmp/logselsecp -a /var/log/$1 /tmp/logsfi

650) this.width=650;" style="background-image:none;border:0px;padding-left:0px;padding-right:0px;padding-top:0px;width:400px;height:153px;" title="wps_clip_image-19798" border="0" alt="wps_clip_image-19798" src="http://img1.51cto.com/attachment/201408/3/8400375_1407046295d3Hp.png" height="153" width="400" hspace="0" vspace="0" />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.