成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

怎樣用Bash編程:邏輯操作符和shell擴(kuò)展

系統(tǒng) Linux
本文是 Bash 編程語(yǔ)言系列的第二篇,探討了 Bash 中文件、字符串、數(shù)字和各種提供流程控制邏輯的邏輯操作符還有不同種類(lèi)的 shell 擴(kuò)展。

怎樣用Bash編程:邏輯操作符和shell擴(kuò)展

學(xué)習(xí)邏輯操作符和 shell 擴(kuò)展,本文是三篇 Bash 編程系列的第二篇。

Bash 是一種強(qiáng)大的編程語(yǔ)言,完美契合命令行和 shell 腳本。本系列(三篇文章,基于我的 三集 Linux 自學(xué)課程)講解如何在 CLI 使用 Bash 編程。

第一篇文章 講解了 Bash 的一些簡(jiǎn)單命令行操作,包括如何使用變量和控制操作符。第二篇文章探討文件、字符串、數(shù)字等類(lèi)型和各種各樣在執(zhí)行流中提供控制邏輯的的邏輯運(yùn)算符,還有 Bash 中的各類(lèi) shell 擴(kuò)展。本系列第三篇也是最后一篇文章,將會(huì)探索能重復(fù)執(zhí)行操作的 forwhileuntil 循環(huán)。

邏輯操作符是程序中進(jìn)行判斷的根本要素,也是執(zhí)行不同的語(yǔ)句組合的依據(jù)。有時(shí)這也被稱(chēng)為流控制。

邏輯操作符

Bash 中有大量的用于不同條件表達(dá)式的邏輯操作符。最基本的是 if 控制結(jié)構(gòu),它判斷一個(gè)條件,如果條件為真,就執(zhí)行一些程序語(yǔ)句。操作符共有三類(lèi):文件、數(shù)字和非數(shù)字操作符。如果條件為真,所有的操作符返回真值(0),如果條件為假,返回假值(1)。

這些比較操作符的函數(shù)語(yǔ)法是,一個(gè)操作符加一個(gè)或兩個(gè)參數(shù)放在中括號(hào)內(nèi),后面跟一系列程序語(yǔ)句,如果條件為真,程序語(yǔ)句執(zhí)行,可能會(huì)有另一個(gè)程序語(yǔ)句列表,該列表在條件為假時(shí)執(zhí)行:

  1. if [ arg1 operator arg2 ] ; then list
  2. if [ arg1 operator arg2 ] ; then list ; else list ; fi

像例子中那樣,在比較表達(dá)式中,空格不能省略。中括號(hào)的每部分,[],是跟 test 命令一樣的傳統(tǒng)的 Bash 符號(hào):

  1. if test arg1 operator arg2 ; then list

還有一個(gè)更新的語(yǔ)法能提供一點(diǎn)點(diǎn)便利,一些系統(tǒng)管理員比較喜歡用。這種格式對(duì)于不同版本的 Bash 和一些 shell 如 ksh(Korn shell)兼容性稍差。格式如下:

  1. if [[ arg1 operator arg2 ]] ; then list

文件操作符

文件操作符是 Bash 中一系列強(qiáng)大的邏輯操作符。圖表 1 列出了 20 多種不同的 Bash 處理文件的操作符。在我的腳本中使用頻率很高。

操作符 描述
-a filename 如果文件存在,返回真值;文件可以為空也可以有內(nèi)容,但是只要它存在,就返回真值
-b filename 如果文件存在且是一個(gè)塊設(shè)備,如 /dev/sda/dev/sda1,則返回真值
-c filename 如果文件存在且是一個(gè)字符設(shè)備,如 /dev/TTY1,則返回真值
-d filename 如果文件存在且是一個(gè)目錄,返回真值
-e filename 如果文件存在,返回真值;與上面的 -a 相同
-f filename 如果文件存在且是一個(gè)一般文件,不是目錄、設(shè)備文件或鏈接等的其他的文件,則返回 真值
-g filename 如果文件存在且 SETGID 標(biāo)記被設(shè)置在其上,返回真值
-h filename 如果文件存在且是一個(gè)符號(hào)鏈接,則返回真值
-k filename 如果文件存在且粘滯位已設(shè)置,則返回真值
-p filename 如果文件存在且是一個(gè)命名的管道(FIFO),返回真值
-r filename 如果文件存在且有可讀權(quán)限(它的可讀位被設(shè)置),返回真值
-s filename 如果文件存在且大小大于 0,返回真值;如果一個(gè)文件存在但大小為 0,則返回假值
-t fd 如果文件描述符 fd 被打開(kāi)且被關(guān)聯(lián)到一個(gè)終端設(shè)備上,返回真值
-u filename 如果文件存在且它的 SETUID 位被設(shè)置,返回真值
-w filename 如果文件存在且有可寫(xiě)權(quán)限,返回真值
-x filename 如果文件存在且有可執(zhí)行權(quán)限,返回真值
-G filename 如果文件存在且文件的組 ID 與當(dāng)前用戶(hù)相同,返回真值
-L filename 如果文件存在且是一個(gè)符號(hào)鏈接,返回真值(同 -h
-N filename 如果文件存在且從文件上一次被讀取后文件被修改過(guò),返回真值
-O filename 如果文件存在且你是文件的擁有者,返回真值
-S filename 如果文件存在且文件是套接字,返回真值
file1 -ef file2 如果文件 file1 和文件 file2 指向同一設(shè)備的同一 INODE 號(hào),返回真值(即硬鏈接)
file1 -nt file2 如果文件 file1file2 新(根據(jù)修改日期),或 file1 存在而 file2 不存在,返回真值
file1 -ot file2 如果文件 file1file2 舊(根據(jù)修改日期),或 file1 不存在而 file2 存在

圖表 1:Bash 文件操作符

以測(cè)試一個(gè)文件存在與否來(lái)舉例:

  1. [student@studentvm1 testdir]$ File="TestFile1" ; if [ -e $File ] ; then echo "The file $File exists." ; else echo "The file $File does not exist." ; fi
  2. The file TestFile1 does not exist.
  3. [student@studentvm1 testdir]$

創(chuàng)建一個(gè)用來(lái)測(cè)試的文件,命名為 TestFile1。目前它不需要包含任何數(shù)據(jù):

  1. [student@studentvm1 testdir]$ touch TestFile1

在這個(gè)簡(jiǎn)短的 CLI 程序中,修改 $File 變量的值相比于在多個(gè)地方修改表示文件名的字符串的值要容易:

  1. [student@studentvm1 testdir]$ File="TestFile1" ; if [ -e $File ] ; then echo "The file $File exists." ; else echo "The file $File does not exist." ; fi
  2. The file TestFile1 exists.
  3. [student@studentvm1 testdir]$

現(xiàn)在,運(yùn)行一個(gè)測(cè)試來(lái)判斷一個(gè)文件是否存在且長(zhǎng)度不為 0(表示它包含數(shù)據(jù))。假設(shè)你想判斷三種情況:

  1. 文件不存在;
  2. 文件存在且為空;
  3. 文件存在且包含數(shù)據(jù)。

因此,你需要一組更復(fù)雜的測(cè)試代碼 — 為了測(cè)試所有的情況,使用 if-elif-else 結(jié)構(gòu)中的 elif 語(yǔ)句:

  1. [student@studentvm1 testdir]$ File="TestFile1" ; if [ -s $File ] ; then echo "$File exists and contains data." ; fi
  2. [student@studentvm1 testdir]$

在這個(gè)情況中,文件存在但不包含任何數(shù)據(jù)。向文件添加一些數(shù)據(jù)再運(yùn)行一次:

  1. [student@studentvm1 testdir]$ File="TestFile1" ; echo "This is file $File" > $File ; if [ -s $File ] ; then echo "$File exists and contains data." ; fi
  2. TestFile1 exists and contains data.
  3. [student@studentvm1 testdir]$

這組語(yǔ)句能返回正常的結(jié)果,但是僅僅是在我們已知三種可能的情況下測(cè)試某種確切的條件。添加一段 else 語(yǔ)句,這樣你就可以更精確地測(cè)試。把文件刪掉,你就可以完整地測(cè)試這段新代碼:

  1. [student@studentvm1 testdir]$ File="TestFile1" ; rm $File ; if [ -s $File ] ; then echo "$File exists and contains data." ; else echo "$File does not exist or is empty." ; fi
  2. TestFile1 does not exist or is empty.

現(xiàn)在創(chuàng)建一個(gè)空文件用來(lái)測(cè)試:

  1. [student@studentvm1 testdir]$ File="TestFile1" ; touch $File ; if [ -s $File ] ; then echo "$File exists and contains data." ; else echo "$File does not exist or is empty." ; fi
  2. TestFile1 does not exist or is empty.

向文件添加一些內(nèi)容,然后再測(cè)試一次:

  1. [student@studentvm1 testdir]$ File="TestFile1" ; echo "This is file $File" > $File ; if [ -s $File ] ; then echo "$File exists and contains data." ; else echo "$File does not exist or is empty." ; fi
  2. TestFile1 exists and contains data.

現(xiàn)在加入 elif 語(yǔ)句來(lái)辨別是文件不存在還是文件為空:

  1. [student@studentvm1 testdir]$ File="TestFile1" ; touch $File ; if [ -s $File ] ; then echo "$File exists and contains data." ; elif [ -e $File ] ; then echo "$File exists and is empty." ; else echo "$File does not exist." ; fi
  2. TestFile1 exists and is empty.
  3. [student@studentvm1 testdir]$ File="TestFile1" ; echo "This is $File" > $File ; if [ -s $File ] ; then echo "$File exists and contains data." ; elif [ -e $File ] ; then echo "$File exists and is empty." ; else echo "$File does not exist." ; fi
  4. TestFile1 exists and contains data.
  5. [student@studentvm1 testdir]$

現(xiàn)在你有一個(gè)可以測(cè)試這三種情況的 Bash CLI 程序,但是可能的情況是無(wú)限的。

如果你能像保存在文件中的腳本那樣組織程序語(yǔ)句,那么即使對(duì)于更復(fù)雜的命令組合也會(huì)很容易看出它們的邏輯結(jié)構(gòu)。圖表 2 就是一個(gè)示例。 if-elif-else 結(jié)構(gòu)中每一部分的程序語(yǔ)句的縮進(jìn)讓邏輯更變得清晰。

  1. File="TestFile1"
  2. echo "This is $File" > $File
  3. if [ -s $File ]
  4. then
  5. echo "$File exists and contains data."
  6. elif [ -e $File ]
  7. then
  8. echo "$File exists and is empty."
  9. else
  10. echo "$File does not exist."
  11. fi

圖表 2: 像在腳本里一樣重寫(xiě)書(shū)寫(xiě)命令行程序

對(duì)于大多數(shù) CLI 程序來(lái)說(shuō),讓這些復(fù)雜的命令變得有邏輯需要寫(xiě)很長(zhǎng)的代碼。雖然 CLI 可能是用 Linux 或 Bash 內(nèi)置的命令,但是當(dāng) CLI 程序很長(zhǎng)或很復(fù)雜時(shí),創(chuàng)建一個(gè)保存在文件中的腳本將更有效,保存到文件中后,可以隨時(shí)運(yùn)行。

字符串比較操作符

字符串比較操作符使我們可以對(duì)字符串中的字符按字母順序進(jìn)行比較。圖表 3 列出了僅有的幾個(gè)字符串比較操作符。

< 如顯示不全,請(qǐng)左右滑動(dòng) >
操作符 描述
-z string 如果字符串的長(zhǎng)度為 0 ,返回真值
-n string 如果字符串的長(zhǎng)度不為 0 ,返回真值
string1 == string2string1 = string2 如果兩個(gè)字符串相等,返回真值。處于遵從 POSIX 一致性,在測(cè)試命令中應(yīng)使用一個(gè)等號(hào) =。與命令 [[ 一起使用時(shí),會(huì)進(jìn)行如上描述的模式匹配(混合命令)。
string1 != string2 兩個(gè)字符串不相等,返回真值
string1 < string2 如果對(duì) string1string2 按字母順序進(jìn)行排序,string1 排在 string2 前面(即基于地區(qū)設(shè)定的對(duì)所有字母和特殊字符的排列順序)
string1 > string2 如果對(duì) string1string2 按字母順序進(jìn)行排序,string1 排在 string2 后面

圖表 3: Bash 字符串邏輯操作符

首先,檢查字符串長(zhǎng)度。比較表達(dá)式中 $MyVar 兩邊的雙引號(hào)不能省略(你仍應(yīng)該在目錄 ~/testdir 下 )。

  1. [student@studentvm1 testdir]$ MyVar="" ; if [ -z "" ] ; then echo "MyVar is zero length." ; else echo "MyVar contains data" ; fi
  2. MyVar is zero length.
  3. [student@studentvm1 testdir]$ MyVar="Random text" ; if [ -z "" ] ; then echo "MyVar is zero length." ; else echo "MyVar contains data" ; fi
  4. MyVar is zero length.

你也可以這樣做:

  1. [student@studentvm1 testdir]$ MyVar="Random text" ; if [ -n "$MyVar" ] ; then echo "MyVar contains data." ; else echo "MyVar is zero length" ; fi
  2. MyVar contains data.
  3. [student@studentvm1 testdir]$ MyVar="" ; if [ -n "$MyVar" ] ; then echo "MyVar contains data." ; else echo "MyVar is zero length" ; fi
  4. MyVar is zero length

有時(shí)候你需要知道一個(gè)字符串確切的長(zhǎng)度。這雖然不是比較,但是也與比較相關(guān)。不幸的是,計(jì)算字符串的長(zhǎng)度沒(méi)有簡(jiǎn)單的方法。有很多種方法可以計(jì)算,但是我認(rèn)為使用 expr(求值表達(dá)式)命令是相對(duì)最簡(jiǎn)單的一種。閱讀 expr 的手冊(cè)頁(yè)可以了解更多相關(guān)知識(shí)。注意表達(dá)式中你檢測(cè)的字符串或變量?jī)蛇叺囊?hào)不要省略。

  1. [student@studentvm1 testdir]$ MyVar="" ; expr length "$MyVar"
  2. 0
  3. [student@studentvm1 testdir]$ MyVar="How long is this?" ; expr length "$MyVar"
  4. 17
  5. [student@studentvm1 testdir]$ expr length "We can also find the length of a literal string as well as a variable."
  6. 70

關(guān)于比較操作符,在我們的腳本中使用了大量的檢測(cè)兩個(gè)字符串是否相等(例如,兩個(gè)字符串是否實(shí)際上是同一個(gè)字符串)的操作。我使用的是非 POSIX 版本的比較表達(dá)式:

  1. [student@studentvm1 testdir]$ Var1="Hello World" ; Var2="Hello World" ; if [ "$Var1" == "$Var2" ] ; then echo "Var1 matches Var2" ; else echo "Var1 and Var2 do not match." ; fi
  2. Var1 matches Var2
  3. [student@studentvm1 testdir]$ Var1="Hello World" ; Var2="Hello world" ; if [ "$Var1" == "$Var2" ] ; then echo "Var1 matches Var2" ; else echo "Var1 and Var2 do not match." ; fi
  4. Var1 and Var2 do not match.

在你自己的腳本中去試一下這些操作符。

數(shù)字比較操作符

數(shù)字操作符用于兩個(gè)數(shù)字參數(shù)之間的比較。像其他類(lèi)操作符一樣,大部分都很容易理解。

操作符 描述
arg1 -eq arg2 如果 arg1 等于 arg2,返回真值
arg1 -ne arg2 如果 arg1 不等于 arg2,返回真值
arg1 -lt arg2 如果 arg1 小于 arg2,返回真值
arg1 -le arg2 如果 arg1 小于或等于 arg2,返回真值
arg1 -gt arg2 如果 arg1 大于 arg2,返回真值
arg1 -ge arg2 如果 arg1 大于或等于 arg2,返回真值

圖表 4: Bash 數(shù)字比較邏輯操作符

來(lái)看幾個(gè)簡(jiǎn)單的例子。第一個(gè)示例設(shè)置變量 $X 的值為 1,然后檢測(cè) $X 是否等于 1。第二個(gè)示例中,$X 被設(shè)置為 0,所以比較表達(dá)式返回結(jié)果不為真值。

  1. [student@studentvm1 testdir]$ X=1 ; if [ $X -eq 1 ] ; then echo "X equals 1" ; else echo "X does not equal 1" ; fi
  2. X equals 1
  3. [student@studentvm1 testdir]$ X=0 ; if [ $X -eq 1 ] ; then echo "X equals 1" ; else echo "X does not equal 1" ; fi
  4. X does not equal 1
  5. [student@studentvm1 testdir]$

自己來(lái)多嘗試一下其他的。

雜項(xiàng)操作符

這些雜項(xiàng)操作符展示一個(gè) shell 選項(xiàng)是否被設(shè)置,或一個(gè) shell 變量是否有值,但是它不顯示變量的值,只顯示它是否有值。

操作符 描述
-o optname 如果一個(gè) shell 選項(xiàng) optname 是啟用的(查看內(nèi)建在 Bash 手冊(cè)頁(yè)中的 set -o 選項(xiàng)描述下面的選項(xiàng)列表),則返回真值
-v varname 如果 shell 變量 varname 被設(shè)置了值(被賦予了值),則返回真值
-R varname 如果一個(gè) shell 變量 varname 被設(shè)置了值且是一個(gè)名字引用,則返回真值

圖表 5: 雜項(xiàng) Bash 邏輯操作符

自己來(lái)使用這些操作符實(shí)踐下。

擴(kuò)展

Bash 支持非常有用的幾種類(lèi)型的擴(kuò)展和命令替換。根據(jù) Bash 手冊(cè)頁(yè),Bash 有七種擴(kuò)展格式。本文只介紹其中五種:~ 擴(kuò)展、算術(shù)擴(kuò)展、路徑名稱(chēng)擴(kuò)展、大括號(hào)擴(kuò)展和命令替換。

大括號(hào)擴(kuò)展

大括號(hào)擴(kuò)展是生成任意字符串的一種方法。(下面的例子是用特定模式的字符創(chuàng)建大量的文件。)大括號(hào)擴(kuò)展可以用于產(chǎn)生任意字符串的列表,并把它們插入一個(gè)用靜態(tài)字符串包圍的特定位置或靜態(tài)字符串的兩端。這可能不太好想象,所以還是來(lái)實(shí)踐一下。

首先,看一下大括號(hào)擴(kuò)展的作用:

  1. [student@studentvm1 testdir]$ echo {string1,string2,string3}
  2. string1 string2 string3

看起來(lái)不是很有用,對(duì)吧?但是用其他方式使用它,再來(lái)看看:

  1. [student@studentvm1 testdir]$ echo "Hello "{David,Jen,Rikki,Jason}.
  2. Hello David. Hello Jen. Hello Rikki. Hello Jason.

這看起來(lái)貌似有點(diǎn)用了 — 我們可以少打很多字。現(xiàn)在試一下這個(gè):

  1. [student@studentvm1 testdir]$ echo b{ed,olt,ar}s
  2. beds bolts bars

我可以繼續(xù)舉例,但是你應(yīng)該已經(jīng)理解了它的用處。

~ 擴(kuò)展

資料顯示,使用最多的擴(kuò)展是波浪字符(~)擴(kuò)展。當(dāng)你在命令中使用它(如 cd ~/Documents)時(shí),Bash shell 把這個(gè)快捷方式展開(kāi)成用戶(hù)的完整的家目錄。

使用這個(gè) Bash 程序觀察 ~ 擴(kuò)展的作用:

  1. [student@studentvm1 testdir]$ echo ~
  2. /home/student
  3. [student@studentvm1 testdir]$ echo ~/Documents
  4. /home/student/Documents
  5. [student@studentvm1 testdir]$ Var1=~/Documents ; echo $Var1 ; cd $Var1
  6. /home/student/Documents
  7. [student@studentvm1 Documents]$

路徑名稱(chēng)擴(kuò)展

路徑名稱(chēng)擴(kuò)展是展開(kāi)文件通配模式為匹配該模式的完整路徑名稱(chēng)的另一種說(shuō)法,匹配字符使用 ?*。文件通配指的是在大量操作中匹配文件名、路徑和其他字符串時(shí)用特定的模式字符產(chǎn)生極大的靈活性。這些特定的模式字符允許匹配字符串中的一個(gè)、多個(gè)或特定字符。

  • ? — 匹配字符串中特定位置的一個(gè)任意字符
  • * — 匹配字符串中特定位置的 0 個(gè)或多個(gè)任意字符

這個(gè)擴(kuò)展用于匹配路徑名稱(chēng)。為了弄清它的用法,請(qǐng)確保 testdir 是當(dāng)前工作目錄(PWD),先執(zhí)行基本的列出清單命令 ls(我家目錄下的內(nèi)容跟你的不一樣)。

  1. [student@studentvm1 testdir]$ ls
  2. chapter6  cpuHog.dos    dmesg1.txt  Documents  Music       softlink1  testdir6    Videos
  3. chapter7  cpuHog.Linux  dmesg2.txt  Downloads  Pictures    Templates  testdir
  4. testdir  cpuHog.mac    dmesg3.txt  file005    Public      testdir    tmp
  5. cpuHog     Desktop       dmesg.txt   link3      random.txt  testdir1   umask.test
  6. [student@studentvm1 testdir]$

現(xiàn)在列出以 Dotestdir/Documentstestdir/Downloads 開(kāi)頭的目錄:

  1. Documents:
  2. Directory01  file07  file15        test02  test10  test20      testfile13  TextFiles
  3. Directory02  file08  file16        test03  test11  testfile01  testfile14
  4. file01       file09  file17        test04  test12  testfile04  testfile15
  5. file02       file10  file18        test05  test13  testfile05  testfile16
  6. file03       file11  file19        test06  test14  testfile09  testfile17
  7. file04       file12  file20        test07  test15  testfile10  testfile18
  8. file05       file13  Student1.txt  test08  test16  testfile11  testfile19
  9. file06       file14  test01        test09  test18  testfile12  testfile20
  10.  
  11. Downloads:
  12. [student@studentvm1 testdir]$

然而,并沒(méi)有得到你期望的結(jié)果。它列出了以 Do 開(kāi)頭的目錄下的內(nèi)容。使用 -d 選項(xiàng),僅列出目錄而不列出它們的內(nèi)容。

  1. [student@studentvm1 testdir]$ ls -d Do*
  2. Documents  Downloads
  3. [student@studentvm1 testdir]$

在兩個(gè)例子中,Bash shell 都把 Do* 模式展開(kāi)成了匹配該模式的目錄名稱(chēng)。但是如果有文件也匹配這個(gè)模式,會(huì)發(fā)生什么?

  1. [student@studentvm1 testdir]$ touch Downtown ; ls -d Do*
  2. Documents  Downloads  Downtown
  3. [student@studentvm1 testdir]$

因此所有匹配這個(gè)模式的文件也被展開(kāi)成了完整名字。

命令替換

命令替換是讓一個(gè)命令的標(biāo)準(zhǔn)輸出數(shù)據(jù)流被當(dāng)做參數(shù)傳給另一個(gè)命令的擴(kuò)展形式,例如,在一個(gè)循環(huán)中作為一系列被處理的項(xiàng)目。Bash 手冊(cè)頁(yè)顯示:“命令替換可以讓你用一個(gè)命令的輸出替換為命令的名字。”這可能不太好理解。

命令替換有兩種格式:`command`$(command)。在更早的格式中使用反引號(hào)(`),在命令中使用反斜杠(\)來(lái)保持它轉(zhuǎn)義之前的文本含義。然而,當(dāng)用在新版本的括號(hào)格式中時(shí),反斜杠被當(dāng)做一個(gè)特殊字符處理。也請(qǐng)注意帶括號(hào)的格式打開(kāi)個(gè)關(guān)閉命令語(yǔ)句都是用一個(gè)括號(hào)。

我經(jīng)常在命令行程序和腳本中使用這種能力,一個(gè)命令的結(jié)果能被用作另一個(gè)命令的參數(shù)。

來(lái)看一個(gè)非常簡(jiǎn)單的示例,這個(gè)示例使用了這個(gè)擴(kuò)展的兩種格式(再一次提醒,確保 testdir 是當(dāng)前工作目錄):

  1. [student@studentvm1 testdir]$ echo "Todays date is `date`"
  2. Todays date is Sun Apr 7 14:42:46 EDT 2019
  3. [student@studentvm1 testdir]$ echo "Todays date is $(date)"
  4. Todays date is Sun Apr 7 14:42:59 EDT 2019
  5. [student@studentvm1 testdir]$

-seq 工具用于一個(gè)數(shù)字序列:

  1. [student@studentvm1 testdir]$ seq 5
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. [student@studentvm1 testdir]$ echo `seq 5`
  8. 1 2 3 4 5
  9. [student@studentvm1 testdir]$

現(xiàn)在你可以做一些更有用處的操作,比如創(chuàng)建大量用于測(cè)試的空文件。

  1. [student@studentvm1 testdir]$ for I in $(seq -w 5000) ; do touch file-$I ; done

seq 工具加上 -w 選項(xiàng)后,在生成的數(shù)字前面會(huì)用 0 補(bǔ)全,這樣所有的結(jié)果都等寬,例如,忽略數(shù)字的值,它們的位數(shù)一樣。這樣在對(duì)它們按數(shù)字順序進(jìn)行排列時(shí)很容易。

seq -w 5000 語(yǔ)句生成了 1 到 5000 的數(shù)字序列。通過(guò)把命令替換用于 for 語(yǔ)句,for 語(yǔ)句就可以使用該數(shù)字序列來(lái)生成文件名的數(shù)字部分。

算術(shù)擴(kuò)展

Bash 可以進(jìn)行整型的數(shù)學(xué)計(jì)算,但是比較繁瑣(你一會(huì)兒將看到)。數(shù)字?jǐn)U展的語(yǔ)法是 $((arithmetic-expression)) ,分別用兩個(gè)括號(hào)來(lái)打開(kāi)和關(guān)閉表達(dá)式。算術(shù)擴(kuò)展在 shell 程序或腳本中類(lèi)似命令替換;表達(dá)式結(jié)算后的結(jié)果替換了表達(dá)式,用于 shell 后續(xù)的計(jì)算。

我們?cè)儆靡粋€(gè)簡(jiǎn)單的用法來(lái)開(kāi)始:

  1. [student@studentvm1 testdir]$ echo $((1+1))
  2. 2
  3. [student@studentvm1 testdir]$ Var1=5 ; Var2=7 ; Var3=$((Var1*Var2)) ; echo "Var 3 = $Var3"
  4. Var 3 = 35

下面的除法結(jié)果是 0,因?yàn)楸磉_(dá)式的結(jié)果是一個(gè)小于 1 的整型數(shù)字:

  1. [student@studentvm1 testdir]$ Var1=5 ; Var2=7 ; Var3=$((Var1/Var2)) ; echo "Var 3 = $Var3"
  2. Var 3 = 0

這是一個(gè)我經(jīng)常在腳本或 CLI 程序中使用的一個(gè)簡(jiǎn)單的計(jì)算,用來(lái)查看在 Linux 主機(jī)中使用了多少虛擬內(nèi)存。 free 不提供我需要的數(shù)據(jù):

  1. [student@studentvm1 testdir]$ RAM=`free | grep ^Mem | awk '{print $2}'` ; Swap=`free | grep ^Swap | awk '{print $2}'` ; echo "RAM = $RAM and Swap = $Swap" ; echo "Total Virtual memory is $((RAM+Swap))" ;
  2. RAM = 4037080 and Swap = 6291452
  3. Total Virtual memory is 10328532

我使用 ` 字符來(lái)劃定用作命令替換的界限。

我用 Bash 算術(shù)擴(kuò)展的場(chǎng)景主要是用腳本檢查系統(tǒng)資源用量后基于返回的結(jié)果選擇一個(gè)程序運(yùn)行的路徑。

總結(jié)

本文是 Bash 編程語(yǔ)言系列的第二篇,探討了 Bash 中文件、字符串、數(shù)字和各種提供流程控制邏輯的邏輯操作符還有不同種類(lèi)的 shell 擴(kuò)展。 

責(zé)任編輯:龐桂玉 來(lái)源: Linux中國(guó)
相關(guān)推薦

2019-12-26 12:47:10

BashLinux命令

2019-11-08 15:10:59

BashBash編程Linux

2010-01-28 11:16:28

C++操作符

2010-07-14 14:55:07

Perl操作符

2009-07-21 09:31:00

Scala數(shù)學(xué)運(yùn)算邏輯操作位操作符

2021-10-31 18:59:55

Python操作符用法

2009-08-19 17:26:28

C# 操作符

2011-04-12 15:03:26

C語(yǔ)言編程位操作

2009-07-21 12:47:04

Scala私有字段定義操作符

2011-04-08 16:26:14

JavaScript

2010-07-19 11:00:24

Perl操作符

2010-07-14 14:30:31

Perl操作符

2009-09-15 17:16:58

LINQ查詢(xún)操作符

2012-02-06 09:13:23

LINQ

2009-09-16 09:09:23

Linq Contai

2010-07-14 14:18:51

Perl操作符

2009-07-21 09:31:00

Scala操作符

2021-01-18 10:15:40

tee命令BashLinux

2019-11-20 12:30:21

Python編程語(yǔ)言語(yǔ)音識(shí)別

2018-04-11 09:30:16

Linux硬盤(pán)分區(qū)parted
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 亚洲 欧美 综合 | 欧美中文字幕在线观看 | 蜜臀久久99精品久久久久野外 | 91传媒在线观看 | 欧美一a一片一级一片 | 99精品久久久 | 日韩一二三区视频 | 欧美激情国产日韩精品一区18 | 91在线| 521av网站 | 干干干日日日 | 国产综合区 | 久久小视频 | 97人人干 | 欧美亚洲国语精品一区二区 | www.天天操| 自拍偷拍中文字幕 | 精品国产乱码久久久久久丨区2区 | 色综合视频 | 午夜影院在线观看版 | 中文字幕 在线观看 | 综合色播 | 欧美v在线观看 | 亚洲不卡视频 | 四虎影视免费观看 | 久久噜噜噜精品国产亚洲综合 | 午夜成人在线视频 | 秋霞影院一区二区 | 中文字字幕在线中文乱码范文 | 久草新视频 | 秋霞a级毛片在线看 | 欧美日韩亚洲国产 | 午夜寂寞影院在线观看 | 欧美成人免费电影 | 日本黄色免费视频 | 黄网站在线播放 | 中文字幕一区二区三区日韩精品 | 亚洲精品一 | 亚洲毛片在线观看 | 久久久久久亚洲精品 | 国内自拍视频在线观看 |