進階bash編程指南(五)

來源:互聯網
上載者:User

一.內部命令與內建命令

1.內建命令指的是包含在Bash工具包中的命令,從字面上看就是built in,主要是考慮到執行效率的問題。內建命令比外部命令執行的更快,一部分原因是外部命令通常都fork出一個子進程來執行,另一部分原因是特定的內建命令可以直接存取shell的核心部分。

2.值得注意的是一個內建命令通常會與一個系統命令同名,但是Bash內部重新實現了這些命令,但是他們在大多數情況下是一樣的

3.關鍵字就是保留字,具有特殊含義,並用來構建shell文法結構。

4.

(1)echo -n "\v\v\v"  #result: \v\v\v  -n選項不換行

(2)echo -e "\v\v\v"  #result: 3個垂直定位字元 -e選項轉義

(3)echo $'\v\v\v'  #result: 3個垂直定位字元

(4)echo "\"dsfadf\"" #result: “dsfadf” 

(5)echo `ls -l`  #刪除"ls -l "命令產生的分行符號

(6)echo "sdffs

fdsdaf"  #換行這裡加-n中間也會有換行,但是最後沒有換行

(7)varr="fdsa

safd"  echo $varr   #換行沒了,換成了空格

5.printf可以實現格式化輸出

6.read

(1)-a  讀取陣列變數

(2)不帶參數的read將讀取的內容存入專用變數$REPLY

(3)輸入給read時,輸入\,然後斷行符號,將會組織產生一個新行。-r選項會讓\轉義 

(4)-s 不列印輸出 -nN 只接受N個字元 -p讀取輸入之前列印後邊的提示符

read -s n1 -p "Hit a key " keypress ,不用輸入斷行符號

(5)方向鍵的編碼

(6)-t指定時間內輸入

(7)通過檔案重新導向來使用read命令

7.set命令

(1)set `uname -a`  #Using set with positional parameters

  echo $1  #`uname -a`結果的第一個字串

  echo $2   

#`uname -a`結果的第二個字串

(2)using set with the -- option explicitly assigns the contents of

a variable to the positional parameters.When no variable follow the

-- ,it unsets the positional parameters.

var="one two"

echo "$1"  #one

echo "$2"  #two

8.unset

the unset command deletes a shell variable ,effectively setting it to null.

Note that this command does not affect positional parameters

9.The export command makes availables to all child processes of the running script or shell.Unfortunately,there is no way to export variables back to the parent process,to

the process that called or invoked the script or shell.One import use of the export command is in startup files,to initialize and make accessible environmental variables to subsequent user processes.

10. jobs命令

Lists the jobs running in the background,giving the job number.

 外部過濾器,程式和命令

二 基本命令

1.ls命令的幾個選項

-R 遞迴選項,將會以分類樹的形式列出所有檔案

-S將會按照檔案尺寸列出所有檔案

-t修改時間

-i顯示檔案的inode

-F標記檔案類型目錄以/結束,檔案*

2.cat,tac

-n:所有行前面插入行號

-b:同-n,但是不對空行進行編號

-s:可以把多個空行壓縮成一個空行

tac,從檔案尾開始顯示

3.rev

(1)bash$ cat file1.txt

This is line 1.

This is line 2.

 (2)bash$ tac file1.txt This is line 2. This is line 1.
  (3)bash$ rev file1.txt .1 enil si sihT .2 enil si sihT

4.mv命令實現重新命名功能

5.rm

-f:強制移除檔案,即使檔案是唯讀

刪除以“-”開頭的檔案時 rm ./-filename or rm -- filename

6.ln [-s] oldfile newfile newfile是oldfile的一個連結

-s選項 軟連結

永久連結:跟原來檔案的inode相同,更改任何檔案的內容都會反映到另一個檔案。,但是

刪除或者重新命名一個檔案,不會影響另一個檔案。

軟連結:不同的inode,但是軟連結可以連結到不同檔案系統的檔案,也可以連結到目錄。

三 複雜命令

1.find命令 

(1)find ~/ -name '*.txt'

(2)find ~/ -mtime 1  #列出最後一天修改的檔案。

(3)find ~/ -name 'core*' -exec rm {}\;  #刪除所有core開頭的檔案

在每一個匹配到的檔案執行exec後面的命令,值得注意的是以\;結束。

";"是轉義符以保證shell傳遞到find命令中的字元不會被即使為其他的

字元;如果exec後面的命令中有{},那麼find命令將會用所有匹配檔案

的路徑名來替換{}

2.head和tail命令

head/tail -n Number filename  #顯示檔案filename的前/後Number行內容

-c N   #顯示檔案的前後N個字元

tail -f log.txt  #顯示檔案的後10行檔案,自動顯示新增的檔案內容,尤其在

監控日誌時,可以在螢幕上一直顯示新增的日誌資訊。

3.expr命令

(1)expr 3 + 5  #返回8 ,注意空格

(2)expr 3 \* 5  #*需要轉義

(3)y=`expr $y + 1` 等價於 let y=y+1 ,y=$(($y+1))

(4)b=`expr $a \> $b`  #測試 ,>,<,>=,<=需要轉義

四 文本處理命令

1.sort inputfile|uniq -c|sort -nr

按行排序檔案inputfile,去掉重複的行並統計每一行在inputfile中出現的

次數,按數位反序進行排序。

2.expand 將每一個tab轉換為一個空格,unexpand相反

3.wc

wc -w 統計單詞數量.

wc -l 統計行數量.

wc -c 統計位元組數量.

wc -m 統計字元數量.

wc -L 給出檔案中最長行的長度.

4.tr 字元轉換過濾器

必須使用引號或中括弧,才是合理的。引用可以阻止shell重新解釋出現在tr命令中的特殊字元。中括弧可以防止被shell擴充。

tr "A-Z" "*" <filename or tr A-Z \* <filename

更通用的做法 tr A-Z '[**]'<filename

五檔案與歸檔命令

1.tar命令

(1)tar -cf 1.tar *.jpg  #將目前的目錄下的所有.jpg檔案打包成1.tar

(2)tar -zf 1.tar  #解開1.tar到目前的目錄下

(3)tar -czf 1.tar *.jpg  #打包並用gzip壓縮,當z換成j時用bzip2壓縮

(4)tar -xzf 1.tar  #(3)的逆過程

(5)其他幾個有用的選項

tar -tf 1.tar  #裡出1.tar中的檔案

tar -uf 1.tar logo.jpg  #更新1.tar中的logo.jpg檔案

tar -rf 1.tar *.gif  #添加所有的檔案到1.tar中去

2.file 確定問加類型

3.which command 確定command的完整路徑

4.whereis command 除了給出完整路徑,還給出該命令man頁的完整路徑

5.whatis 列出系統命令和重要設定檔的解釋。相當與man命令結果的NAME

項的內容

6.vdir=ls -l

7.locate filename 查詢filename的路徑

8.readlink  顯示符號連結所指向的檔案

9.diff 檔案比較工具

--side-by-side選項,左右分割形式輸出,-c -u選項

patch

cmp命令只會指出那些位置不同,不會顯示具體細節

10.basename 去掉路徑資訊,只輸出檔案名

dirname 去掉檔案名稱,只輸出路徑資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.