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

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

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

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

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

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

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

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

邏輯操作符

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

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

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

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

  1. if test arg1 operator arg2 ; then list

還有一個(gè)更新的語法能提供一點(diǎn)點(diǎn)便利,一些系統(tǒng)管理員比較喜歡用。這種格式對于不同版本的 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è)符號鏈接,則返回真值
-k filename 如果文件存在且粘滯位已設(shè)置,則返回真值
-p filename 如果文件存在且是一個(gè)命名的管道(FIFO),返回真值
-r filename 如果文件存在且有可讀權(quán)限(它的可讀位被設(shè)置),返回真值
-s filename 如果文件存在且大小大于 0,返回真值;如果一個(gè)文件存在但大小為 0,則返回假值
-t fd 如果文件描述符 fd 被打開且被關(guān)聯(lián)到一個(gè)終端設(shè)備上,返回真值
-u filename 如果文件存在且它的 SETUID 位被設(shè)置,返回真值
-w filename 如果文件存在且有可寫權(quán)限,返回真值
-x filename 如果文件存在且有可執(zhí)行權(quán)限,返回真值
-G filename 如果文件存在且文件的組 ID 與當(dāng)前用戶相同,返回真值
-L filename 如果文件存在且是一個(gè)符號鏈接,返回真值(同 -h
-N filename 如果文件存在且從文件上一次被讀取后文件被修改過,返回真值
-O filename 如果文件存在且你是文件的擁有者,返回真值
-S filename 如果文件存在且文件是套接字,返回真值
file1 -ef file2 如果文件 file1 和文件 file2 指向同一設(shè)備的同一 INODE 號,返回真值(即硬鏈接)
file1 -nt file2 如果文件 file1file2 新(根據(jù)修改日期),或 file1 存在而 file2 不存在,返回真值
file1 -ot file2 如果文件 file1file2 舊(根據(jù)修改日期),或 file1 不存在而 file2 存在

圖表 1:Bash 文件操作符

以測試一個(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 does not exist.
  3. [student@studentvm1 testdir]$

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

  1. [student@studentvm1 testdir]$ touch TestFile1

在這個(gè)簡短的 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è)測試來判斷一個(gè)文件是否存在且長度不為 0(表示它包含數(shù)據(jù))。假設(shè)你想判斷三種情況:

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

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

  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]$

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

  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è)空文件用來測試:

  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)容,然后再測試一次:

  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 語句來辨別是文件不存在還是文件為空:

  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è)可以測試這三種情況的 Bash CLI 程序,但是可能的情況是無限的。

如果你能像保存在文件中的腳本那樣組織程序語句,那么即使對于更復(fù)雜的命令組合也會(huì)很容易看出它們的邏輯結(jié)構(gòu)。圖表 2 就是一個(gè)示例。 if-elif-else 結(jié)構(gòu)中每一部分的程序語句的縮進(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: 像在腳本里一樣重寫書寫命令行程序

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

字符串比較操作符

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

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

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

首先,檢查字符串長度。比較表達(dá)式中 $MyVar 兩邊的雙引號不能省略(你仍應(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è)字符串確切的長度。這雖然不是比較,但是也與比較相關(guān)。不幸的是,計(jì)算字符串的長度沒有簡單的方法。有很多種方法可以計(jì)算,但是我認(rèn)為使用 expr(求值表達(dá)式)命令是相對最簡單的一種。閱讀 expr 的手冊頁可以了解更多相關(guān)知識。注意表達(dá)式中你檢測的字符串或變量兩邊的引號不要省略。

  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)于比較操作符,在我們的腳本中使用了大量的檢測兩個(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ù)之間的比較。像其他類操作符一樣,大部分都很容易理解。

操作符 描述
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ù)字比較邏輯操作符

來看幾個(gè)簡單的例子。第一個(gè)示例設(shè)置變量 $X 的值為 1,然后檢測 $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]$

自己來多嘗試一下其他的。

雜項(xiàng)操作符

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

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

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

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

擴(kuò)展

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

大括號擴(kuò)展

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

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

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

看起來不是很有用,對吧?但是用其他方式使用它,再來看看:

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

這看起來貌似有點(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è)快捷方式展開成用戶的完整的家目錄。

使用這個(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]$

路徑名稱擴(kuò)展

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

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

這個(gè)擴(kuò)展用于匹配路徑名稱。為了弄清它的用法,請確保 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 開頭的目錄:

  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]$

然而,并沒有得到你期望的結(jié)果。它列出了以 Do 開頭的目錄下的內(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* 模式展開成了匹配該模式的目錄名稱。但是如果有文件也匹配這個(gè)模式,會(huì)發(fā)生什么?

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

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

命令替換

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

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

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

來看一個(gè)非常簡單的示例,這個(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)建大量用于測試的空文件。

  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ù)一樣。這樣在對它們按數(shù)字順序進(jìn)行排列時(shí)很容易。

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

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

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

我們再用一個(gè)簡單的用法來開始:

  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è)簡單的計(jì)算,用來查看在 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

我使用 ` 字符來劃定用作命令替換的界限。

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

總結(jié)

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

責(zé)任編輯:龐桂玉 來源: Linux中國
相關(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語言編程位操作

2009-07-21 12:47:04

Scala私有字段定義操作符

2011-04-08 16:26:14

JavaScript

2010-07-14 14:30:31

Perl操作符

2010-07-19 11:00:24

Perl操作符

2009-07-21 09:31:00

Scala操作符

2009-09-15 17:16:58

LINQ查詢操作符

2012-02-06 09:13:23

LINQ

2009-09-16 09:09:23

Linq Contai

2010-07-14 14:18:51

Perl操作符

2021-01-18 10:15:40

tee命令BashLinux

2024-01-07 20:15:49

Python編程語言

2009-08-21 09:30:05

is和as操作符
點(diǎn)贊
收藏

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

主站蜘蛛池模板: 国产精品v | 精精国产xxxx视频在线播放7 | 免费日韩网站 | 日韩精品一区二区三区在线播放 | 精品国产一区二区三区免费 | 精品国产乱码久久久久久丨区2区 | 国产成人福利在线观看 | 在线观看av网站永久 | 动漫www.被爆羞羞av44 | 欧美日韩亚洲国产 | 一区二区三区久久久 | 国产精品久久久av | 久久精品黄色 | 日韩影院在线观看 | 成人久久久久 | 精品国产一区二区三区久久狼黑人 | 成人av在线播放 | 最新av片 | 亚洲国产第一页 | 欧美日韩高清在线观看 | 亚洲网站在线播放 | 最新国产视频 | 国产精品久久久久一区二区三区 | 欧美日韩视频在线播放 | 久久精品视频播放 | 亚洲自拍偷拍视频 | 在线看av的网址 | 久久国产高清视频 | 国产区在线免费观看 | 亚洲天堂精品一区 | 中文二区 | 成人黄色电影在线播放 | 久久av网 | 亚洲成人一区二区三区 | 国产成人综合久久 | av毛片在线播放 | 伊人国产精品 | 精品国产一区二区国模嫣然 | 草草视频在线免费观看 | 精产国产伦理一二三区 | 欧美天天视频 |