Shell 脚本知识回顾 (三) —— 替换、运算符、字符串、数组
一、Shell替換:Shell變量替換,命令替換,轉(zhuǎn)義字符
如果表達(dá)式中包含特殊字符,Shell 將會(huì)進(jìn)行替換。例如,在雙引號(hào)中使用變量就是一種替換,轉(zhuǎn)義字符也是一種替換。
舉個(gè)例子: [cpp]?view plaincopy下面的轉(zhuǎn)義字符都可以用在 echo 中:
| \\ | 反斜杠 |
| \a | 警報(bào),響鈴 |
| \b | 退格(刪除鍵) |
| \f | 換頁(FF),將當(dāng)前位置移到下頁開頭 |
| \n | 換行 |
| \r | 回車 |
| \t | 水平制表符(tab鍵)? |
| \v | 垂直制表符 |
命令替換
命令替換是指Shell可以先執(zhí)行命令,將輸出結(jié)果暫時(shí)保存,在適當(dāng)?shù)牡胤捷敵觥?/span>命令替換的語法:
[cpp]?view plaincopy
下面的例子中,將命令執(zhí)行結(jié)果保存在變量中:
[cpp]?view plaincopy
變量替換
變量替換可以根據(jù)變量的狀態(tài)(是否為空、是否定義等)來改變它的值?可以使用的變量替換形式:| ${var} | 變量本來的值 |
| ${var:-word} | 如果變量 var 為空或已被刪除(unset),那么返回?word,但不改變?var 的值。 |
| ${var:=word} | 如果變量 var 為空或已被刪除(unset),那么返回 word,并將 var 的值設(shè)置為 word。 |
| ${var:?message} | 如果變量 var 為空或已被刪除(unset),那么將消息 message 送到標(biāo)準(zhǔn)錯(cuò)誤輸出,可以用來檢測(cè)變量 var 是否可以被正常賦值。 若此替換出現(xiàn)在Shell腳本中,那么腳本將停止運(yùn)行。 |
| ${var:+word} | 如果變量 var 被定義,那么返回 word,但不改變 var 的值。 |
請(qǐng)看下面的例子:#!/bin/bashecho ${var:-"Variable is not set"} echo "1 - Value of var is ${var}"echo ${var:="Variable is not set"} echo "2 - Value of var is ${var}"unset var echo ${var:+"This is default value"} echo "3 - Value of var is $var"var="Prefix" echo ${var:+"This is default value"} echo "4 - Value of var is $var"echo ${var:?"Print this message"} echo "5 - Value of var is ${var}"運(yùn)行結(jié)果: [cpp]?view plaincopy
二、Shell運(yùn)算符:Shell算數(shù)運(yùn)算符、關(guān)系運(yùn)算符、布爾運(yùn)算符、字符串運(yùn)算符等
Bash 支持很多運(yùn)算符,包括算數(shù)運(yùn)算符、關(guān)系運(yùn)算符、布爾運(yùn)算符、字符串運(yùn)算符和文件測(cè)試運(yùn)算符。
原生bash不支持簡(jiǎn)單的數(shù)學(xué)運(yùn)算,但是可以通過其他命令來實(shí)現(xiàn),例如 awk 和 expr,expr 最常用。
expr 是一款表達(dá)式計(jì)算工具,使用它能完成表達(dá)式的求值操作。
例如,兩個(gè)數(shù)相加:
[cpp]?view plaincopy
運(yùn)行腳本輸出:
Total value : 4 兩點(diǎn)注意:- 表達(dá)式和運(yùn)算符之間要有空格,例如 2+2 是不對(duì)的,必須寫成 2 + 2,這與我們熟悉的大多數(shù)編程語言不一樣。
- 完整的表達(dá)式要被 ` ` 包含,注意這個(gè)字符不是常用的單引號(hào),在 Esc 鍵下邊。
算術(shù)運(yùn)算符
先來看一個(gè)使用算術(shù)運(yùn)算符的例子[cpp]?view plaincopy
運(yùn)行結(jié)果: a + b : 30 a - b : -10 a * b : 200 b / a : 2 b % a : 0 a is not equal to b 注意:
- 乘號(hào)(*)前邊必須加反斜杠(\)才能實(shí)現(xiàn)乘法運(yùn)算;
- if...then...fi 是條件語句,后續(xù)將會(huì)講解。
| + | 加法 | `expr $a + $b` 結(jié)果為?30。 |
| - | 減法 | `expr $a - $b` 結(jié)果為 10。 |
| * | 乘法 | `expr $a \* $b` 結(jié)果為 ?200。 |
| / | 除法 | `expr $b / $a` 結(jié)果為?2。 |
| % | 取余 | `expr $b % $a` 結(jié)果為?0。 |
| = | 賦值 | a=$b 將把變量 b 的值賦給 a。 |
| == | 相等。用于比較兩個(gè)數(shù)字,相同則返回 true。 | [ $a == $b ] 返回?false。 |
| != | 不相等。用于比較兩個(gè)數(shù)字,不相同則返回 true。 | [ $a != $b ] 返回 true。 |
注意:條件表達(dá)式要放在方括號(hào)之間,并且要有空格,例如?[$a==$b] 是錯(cuò)誤的,必須寫成?[ $a == $b ]。
關(guān)系運(yùn)算符
關(guān)系運(yùn)算符只支持?jǐn)?shù)字,不支持字符串,除非字符串的值是數(shù)字。先來看一個(gè)關(guān)系運(yùn)算符的例子: [cpp]?view plaincopy
運(yùn)行結(jié)果:
10 -eq 20: a is not equal to b 10 -ne 20: a is not equal to b 10 -gt 20: a is not greater than b 10 -lt 20: a is less than b 10 -ge 20: a is not greater or equal to b 10 -le 20: a is less or equal to b| -eq | 檢測(cè)兩個(gè)數(shù)是否相等,相等返回 true。 | [ $a -eq $b ] 返回?true。 |
| -ne | 檢測(cè)兩個(gè)數(shù)是否相等,不相等返回 true。 | [ $a -ne $b ] 返回 true。 |
| -gt | 檢測(cè)左邊的數(shù)是否大于右邊的,如果是,則返回 true。 | [ $a -gt $b ] 返回 false。 |
| -lt | 檢測(cè)左邊的數(shù)是否小于右邊的,如果是,則返回 true。 | [ $a -lt $b ] 返回 true。 |
| -ge | 檢測(cè)左邊的數(shù)是否大等于右邊的,如果是,則返回 true。 | [ $a -ge $b ] 返回 false。 |
| -le | 檢測(cè)左邊的數(shù)是否小于等于右邊的,如果是,則返回 true。 | [ $a -le $b ] 返回 true。 |
布爾運(yùn)算符
先來看一個(gè)布爾運(yùn)算符的例子:[cpp]?view plaincopy
| ! | 非運(yùn)算,表達(dá)式為 true 則返回 false,否則返回 true。 | [ ! false ] 返回 true。 |
| -o | 或運(yùn)算,有一個(gè)表達(dá)式為 true 則返回 true。 | [ $a -lt 20 -o $b -gt 100 ] 返回?true。 |
| -a | 與運(yùn)算,兩個(gè)表達(dá)式都為 true 才返回 true。 | [ $a -lt 20 -a $b -gt 100 ] 返回?false。 |
字符串運(yùn)算符
先來看一個(gè)例子:[cpp]?view plaincopy
運(yùn)行結(jié)果:
abc = efg: a is not equal to b abc != efg : a is not equal to b -z abc : string length is not zero -n abc : string length is not zero abc : string is not empty| = | 檢測(cè)兩個(gè)字符串是否相等,相等返回 true。 | [ $a = $b ] 返回 false。 |
| != | 檢測(cè)兩個(gè)字符串是否相等,不相等返回 true。 | [ $a != $b ] 返回?true。 |
| -z | 檢測(cè)字符串長(zhǎng)度是否為0,為0返回 true。 | [ -z $a ] 返回 false。 |
| -n | 檢測(cè)字符串長(zhǎng)度是否為0,不為0返回 true。 | [ -z $a ] 返回 true。 |
| str | 檢測(cè)字符串是否為空,不為空返回 true。 | [ $a ] 返回?true。 |
文件測(cè)試運(yùn)算符
文件測(cè)試運(yùn)算符用于檢測(cè) Unix 文件的各種屬性。例如,變量 file 表示文件“/var/www/tutorialspoint/unix/test.sh”,它的大小為100字節(jié),具有 rwx 權(quán)限。下面的代碼,將檢測(cè)該文件的各種屬性:
[cpp]?view plaincopyFile has read access File has write permission File has execute permission File is an ordinary file This is not a directory File size is zero File exists
| -b file | 檢測(cè)文件是否是塊設(shè)備文件,如果是,則返回 true。 | [ -b $file ] 返回 false。 |
| -c file | 檢測(cè)文件是否是字符設(shè)備文件,如果是,則返回 true。 | [ -b $file ] 返回?false。 |
| -d file | 檢測(cè)文件是否是目錄,如果是,則返回 true。 | [ -d $file ] 返回 false。 |
| -f file | 檢測(cè)文件是否是普通文件(既不是目錄,也不是設(shè)備文件),如果是,則返回 true。 | [ -f $file ] 返回?true。 |
| -g file | 檢測(cè)文件是否設(shè)置了 SGID 位,如果是,則返回 true。 | [ -g $file ] 返回?false。 |
| -k file | 檢測(cè)文件是否設(shè)置了粘著位(Sticky Bit),如果是,則返回 true。 | [ -k $file ] 返回?false。 |
| -p file | 檢測(cè)文件是否是具名管道,如果是,則返回 true。 | [ -p $file ] 返回?false。 |
| -u file | 檢測(cè)文件是否設(shè)置了 SUID 位,如果是,則返回 true。 | [ -u $file ] 返回?false。 |
| -r file | 檢測(cè)文件是否可讀,如果是,則返回 true。 | [ -r $file ] 返回?true。 |
| -w file | 檢測(cè)文件是否可寫,如果是,則返回 true。 | [ -w $file ] 返回?true。 |
| -x file | 檢測(cè)文件是否可執(zhí)行,如果是,則返回 true。 | [ -x $file ] 返回?true。 |
| -s file | 檢測(cè)文件是否為空(文件大小是否大于0),不為空返回 true。 | [ -s $file ] 返回?true。 |
| -e file | 檢測(cè)文件(包括目錄)是否存在,如果是,則返回 true。 | [ -e $file ] 返回?true。 |
三、Shell字符串
字符串是shell編程中最常用最有用的數(shù)據(jù)類型(除了數(shù)字和字符串,也沒啥其它類型好用了),字符串可以用單引號(hào),也可以用雙引號(hào),也可以不用引號(hào)。單雙引號(hào)的區(qū)別跟PHP類似。
單引號(hào)
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_variable" style="color: rgb(0, 0, 255);">str</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_string" style="color: rgb(24, 97, 167);">'this is a string'</span></li></ol> 單引號(hào)字符串的限制:- 單引號(hào)里的任何字符都會(huì)原樣輸出,單引號(hào)字符串中的變量是無效的;
- 單引號(hào)字串中不能出現(xiàn)單引號(hào)(對(duì)單引號(hào)使用轉(zhuǎn)義符后也不行)。
雙引號(hào)
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_variable" style="color: rgb(0, 0, 255);">your_name</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_string" style="color: rgb(24, 97, 167);">'qinjx'</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_variable" style="color: rgb(0, 0, 255);">str</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Hello, I know your are </span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\"</span><span class="sh_string" style="color: rgb(24, 97, 167);">$your_name</span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\"</span><span class="sh_string" style="color: rgb(24, 97, 167);">! </span><span class="sh_specialchar" style="color: rgb(24, 97, 167);">\n</span><span class="sh_string" style="color: rgb(24, 97, 167);">"</span></li></ol> 雙引號(hào)的優(yōu)點(diǎn):- 雙引號(hào)里可以有變量
- 雙引號(hào)里可以出現(xiàn)轉(zhuǎn)義字符
拼接字符串
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_variable" style="color: rgb(0, 0, 255);">your_name</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_string" style="color: rgb(24, 97, 167);">"qinjx"</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_variable" style="color: rgb(0, 0, 255);">greeting</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_string" style="color: rgb(24, 97, 167);">"hello, "</span><span class="sh_variable" style="color: rgb(0, 0, 255);">$your_name</span><span class="sh_string" style="color: rgb(24, 97, 167);">" !"</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_variable" style="color: rgb(0, 0, 255);">greeting_1</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_string" style="color: rgb(24, 97, 167);">"hello, ${your_name} !"</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">echo <span class="sh_variable" style="color: rgb(0, 0, 255);">$greeting</span> <span class="sh_variable" style="color: rgb(0, 0, 255);">$greeting_1</span></li></ol>獲取字符串長(zhǎng)度
復(fù)制純文本新窗口 <ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_variable" style="color: rgb(0, 0, 255);">string</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_string" style="color: rgb(24, 97, 167);">"abcd"</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">echo <span class="sh_variable" style="color: rgb(0, 0, 255);">${#string}</span> <span class="sh_comment" style="color: rgb(56, 173, 36);">#輸出 4</span></li></ol>提取子字符串
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_variable" style="color: rgb(0, 0, 255);">string</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_string" style="color: rgb(24, 97, 167);">"alibaba is a great company"</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">echo <span class="sh_variable" style="color: rgb(0, 0, 255);">${string:1:4}</span> <span class="sh_comment" style="color: rgb(56, 173, 36);">#輸出liba</span></li></ol>查找子字符串
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_variable" style="color: rgb(0, 0, 255);">string</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_string" style="color: rgb(24, 97, 167);">"alibaba is a great company"</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">echo `expr index <span class="sh_string" style="color: rgb(24, 97, 167);">"$string"</span> is`</li></ol>
四、Shell數(shù)組:shell數(shù)組的定義、數(shù)組長(zhǎng)度
Shell在編程方面比Windows批處理強(qiáng)大很多,無論是在循環(huán)、運(yùn)算。
bash支持一維數(shù)組(不支持多維數(shù)組),并且沒有限定數(shù)組的大小。類似與C語言,數(shù)組元素的下標(biāo)由0開始編號(hào)。獲取數(shù)組中的元素要利用下標(biāo),下標(biāo)可以是整數(shù)或算術(shù)表達(dá)式,其值應(yīng)大于或等于0。
定義數(shù)組
在Shell中,用括號(hào)來表示數(shù)組,數(shù)組元素用“空格”符號(hào)分割開。定義數(shù)組的一般形式為:? ??array_name=(value1 ... valuen)
例如:
<ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_variable" style="color: rgb(0, 0, 255);">array_name</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">=(</span>value0 value1 value2 value3<span class="sh_symbol" style="color: rgb(48, 48, 238);">)</span></li></ol> 或者 <ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_variable" style="color: rgb(0, 0, 255);">array_name</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">=(</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">value0</li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">value1</li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">value2</li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">value3</li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_symbol" style="color: rgb(48, 48, 238);">)</span></li></ol>
還可以單獨(dú)定義數(shù)組的各個(gè)分量: <ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">array_name<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]=</span>value0</li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">array_name<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">1</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]=</span>value1</li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">array_name<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">2</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]=</span>value2</li></ol> 可以不使用連續(xù)的下標(biāo),而且下標(biāo)的范圍沒有限制。
讀取數(shù)組
讀取數(shù)組元素值的一般格式是:? ??${array_name[index]}
例如: <ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_variable" style="color: rgb(0, 0, 255);">valuen</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_variable" style="color: rgb(0, 0, 255);">${array_name[2]}</span></li></ol> 舉個(gè)例子: <ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_comment" style="color: rgb(56, 173, 36);">#!/bin/sh</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">NAME<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]=</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Zara"</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">NAME<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">1</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]=</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Qadir"</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">NAME<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">2</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]=</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Mahnaz"</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">NAME<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">3</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]=</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Ayan"</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">NAME<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">4</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]=</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Daisy"</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">echo <span class="sh_string" style="color: rgb(24, 97, 167);">"First Index: ${NAME[0]}"</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">echo <span class="sh_string" style="color: rgb(24, 97, 167);">"Second Index: ${NAME[1]}"</span></li></ol> 運(yùn)行腳本,輸出: $./test.sh First Index: Zara Second Index: Qadir 使用@ 或 * 可以獲取數(shù)組中的所有元素,例如: <ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_variable" style="color: rgb(0, 0, 255);">${array_name[*]}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_variable" style="color: rgb(0, 0, 255);">${array_name[@]}</span></li></ol> 舉個(gè)例子: <ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_comment" style="color: rgb(56, 173, 36);">#!/bin/sh</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">NAME<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">0</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]=</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Zara"</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">NAME<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">1</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]=</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Qadir"</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">NAME<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">2</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]=</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Mahnaz"</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">NAME<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">3</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]=</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Ayan"</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">NAME<span class="sh_symbol" style="color: rgb(48, 48, 238);">[</span><span class="sh_number" style="color: rgb(50, 186, 6);">4</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">]=</span><span class="sh_string" style="color: rgb(24, 97, 167);">"Daisy"</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">echo <span class="sh_string" style="color: rgb(24, 97, 167);">"First Method: ${NAME[*]}"</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;">echo <span class="sh_string" style="color: rgb(24, 97, 167);">"Second Method: ${NAME[@]}"</span></li></ol> 運(yùn)行腳本,輸出: $./test.sh First Method: Zara Qadir Mahnaz Ayan Daisy Second Method: Zara Qadir Mahnaz Ayan Daisy
獲取數(shù)組的長(zhǎng)度
獲取數(shù)組長(zhǎng)度的方法與獲取字符串長(zhǎng)度的方法相同,例如: <ol class="snippet-num" style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; padding-left: 20px; padding-top: 3px !important; padding-right: 8px !important; padding-bottom: 3px !important;"><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_comment" style="color: rgb(56, 173, 36);"># 取得數(shù)組元素的個(gè)數(shù)</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_variable" style="color: rgb(0, 0, 255);">length</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_variable" style="color: rgb(0, 0, 255);">${#array_name[@]}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_comment" style="color: rgb(56, 173, 36);"># 或者</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_variable" style="color: rgb(0, 0, 255);">length</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_variable" style="color: rgb(0, 0, 255);">${#array_name[*]}</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_comment" style="color: rgb(56, 173, 36);"># 取得數(shù)組單個(gè)元素的長(zhǎng)度</span></li><li style="padding-left: 8px; list-style: decimal-leading-zero outside none;"><span class="sh_variable" style="color: rgb(0, 0, 255);">lengthn</span><span class="sh_symbol" style="color: rgb(48, 48, 238);">=</span><span class="sh_variable" style="color: rgb(0, 0, 255);">${#array_name[n]}</span></li></ol>總結(jié)
以上是生活随笔為你收集整理的Shell 脚本知识回顾 (三) —— 替换、运算符、字符串、数组的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学习笔记(二)JavaScript基本概
- 下一篇: 程序员学好英语的方法(转)