Shell 脚本知识回顾 (三) —— 替换、运算符、字符串、数组
生活随笔
收集整理的這篇文章主要介紹了
Shell 脚本知识回顾 (三) —— 替换、运算符、字符串、数组
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、Shell替換:Shell變量替換,命令替換,轉義字符
如果表達式中包含特殊字符,Shell 將會進行替換。例如,在雙引號中使用變量就是一種替換,轉義字符也是一種替換。
舉個例子: [cpp]?view plaincopy下面的轉義字符都可以用在 echo 中:
| \\ | 反斜杠 |
| \a | 警報,響鈴 |
| \b | 退格(刪除鍵) |
| \f | 換頁(FF),將當前位置移到下頁開頭 |
| \n | 換行 |
| \r | 回車 |
| \t | 水平制表符(tab鍵)? |
| \v | 垂直制表符 |
命令替換
命令替換是指Shell可以先執行命令,將輸出結果暫時保存,在適當的地方輸出。命令替換的語法:
[cpp]?view plaincopy
下面的例子中,將命令執行結果保存在變量中:
[cpp]?view plaincopy
變量替換
變量替換可以根據變量的狀態(是否為空、是否定義等)來改變它的值?可以使用的變量替換形式:| ${var} | 變量本來的值 |
| ${var:-word} | 如果變量 var 為空或已被刪除(unset),那么返回?word,但不改變?var 的值。 |
| ${var:=word} | 如果變量 var 為空或已被刪除(unset),那么返回 word,并將 var 的值設置為 word。 |
| ${var:?message} | 如果變量 var 為空或已被刪除(unset),那么將消息 message 送到標準錯誤輸出,可以用來檢測變量 var 是否可以被正常賦值。 若此替換出現在Shell腳本中,那么腳本將停止運行。 |
| ${var:+word} | 如果變量 var 被定義,那么返回 word,但不改變 var 的值。 |
請看下面的例子:#!/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}"運行結果: [cpp]?view plaincopy
二、Shell運算符:Shell算數運算符、關系運算符、布爾運算符、字符串運算符等
Bash 支持很多運算符,包括算數運算符、關系運算符、布爾運算符、字符串運算符和文件測試運算符。
原生bash不支持簡單的數學運算,但是可以通過其他命令來實現,例如 awk 和 expr,expr 最常用。
expr 是一款表達式計算工具,使用它能完成表達式的求值操作。
例如,兩個數相加:
[cpp]?view plaincopy
運行腳本輸出:
Total value : 4 兩點注意:- 表達式和運算符之間要有空格,例如 2+2 是不對的,必須寫成 2 + 2,這與我們熟悉的大多數編程語言不一樣。
- 完整的表達式要被 ` ` 包含,注意這個字符不是常用的單引號,在 Esc 鍵下邊。
算術運算符
先來看一個使用算術運算符的例子[cpp]?view plaincopy
運行結果: a + b : 30 a - b : -10 a * b : 200 b / a : 2 b % a : 0 a is not equal to b 注意:
- 乘號(*)前邊必須加反斜杠(\)才能實現乘法運算;
- if...then...fi 是條件語句,后續將會講解。
| + | 加法 | `expr $a + $b` 結果為?30。 |
| - | 減法 | `expr $a - $b` 結果為 10。 |
| * | 乘法 | `expr $a \* $b` 結果為 ?200。 |
| / | 除法 | `expr $b / $a` 結果為?2。 |
| % | 取余 | `expr $b % $a` 結果為?0。 |
| = | 賦值 | a=$b 將把變量 b 的值賦給 a。 |
| == | 相等。用于比較兩個數字,相同則返回 true。 | [ $a == $b ] 返回?false。 |
| != | 不相等。用于比較兩個數字,不相同則返回 true。 | [ $a != $b ] 返回 true。 |
注意:條件表達式要放在方括號之間,并且要有空格,例如?[$a==$b] 是錯誤的,必須寫成?[ $a == $b ]。
關系運算符
關系運算符只支持數字,不支持字符串,除非字符串的值是數字。先來看一個關系運算符的例子: [cpp]?view plaincopy
運行結果:
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 | 檢測兩個數是否相等,相等返回 true。 | [ $a -eq $b ] 返回?true。 |
| -ne | 檢測兩個數是否相等,不相等返回 true。 | [ $a -ne $b ] 返回 true。 |
| -gt | 檢測左邊的數是否大于右邊的,如果是,則返回 true。 | [ $a -gt $b ] 返回 false。 |
| -lt | 檢測左邊的數是否小于右邊的,如果是,則返回 true。 | [ $a -lt $b ] 返回 true。 |
| -ge | 檢測左邊的數是否大等于右邊的,如果是,則返回 true。 | [ $a -ge $b ] 返回 false。 |
| -le | 檢測左邊的數是否小于等于右邊的,如果是,則返回 true。 | [ $a -le $b ] 返回 true。 |
布爾運算符
先來看一個布爾運算符的例子:[cpp]?view plaincopy
| ! | 非運算,表達式為 true 則返回 false,否則返回 true。 | [ ! false ] 返回 true。 |
| -o | 或運算,有一個表達式為 true 則返回 true。 | [ $a -lt 20 -o $b -gt 100 ] 返回?true。 |
| -a | 與運算,兩個表達式都為 true 才返回 true。 | [ $a -lt 20 -a $b -gt 100 ] 返回?false。 |
字符串運算符
先來看一個例子:[cpp]?view plaincopy
運行結果:
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| = | 檢測兩個字符串是否相等,相等返回 true。 | [ $a = $b ] 返回 false。 |
| != | 檢測兩個字符串是否相等,不相等返回 true。 | [ $a != $b ] 返回?true。 |
| -z | 檢測字符串長度是否為0,為0返回 true。 | [ -z $a ] 返回 false。 |
| -n | 檢測字符串長度是否為0,不為0返回 true。 | [ -z $a ] 返回 true。 |
| str | 檢測字符串是否為空,不為空返回 true。 | [ $a ] 返回?true。 |
文件測試運算符
文件測試運算符用于檢測 Unix 文件的各種屬性。例如,變量 file 表示文件“/var/www/tutorialspoint/unix/test.sh”,它的大小為100字節,具有 rwx 權限。下面的代碼,將檢測該文件的各種屬性:
[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 | 檢測文件是否是塊設備文件,如果是,則返回 true。 | [ -b $file ] 返回 false。 |
| -c file | 檢測文件是否是字符設備文件,如果是,則返回 true。 | [ -b $file ] 返回?false。 |
| -d file | 檢測文件是否是目錄,如果是,則返回 true。 | [ -d $file ] 返回 false。 |
| -f file | 檢測文件是否是普通文件(既不是目錄,也不是設備文件),如果是,則返回 true。 | [ -f $file ] 返回?true。 |
| -g file | 檢測文件是否設置了 SGID 位,如果是,則返回 true。 | [ -g $file ] 返回?false。 |
| -k file | 檢測文件是否設置了粘著位(Sticky Bit),如果是,則返回 true。 | [ -k $file ] 返回?false。 |
| -p file | 檢測文件是否是具名管道,如果是,則返回 true。 | [ -p $file ] 返回?false。 |
| -u file | 檢測文件是否設置了 SUID 位,如果是,則返回 true。 | [ -u $file ] 返回?false。 |
| -r file | 檢測文件是否可讀,如果是,則返回 true。 | [ -r $file ] 返回?true。 |
| -w file | 檢測文件是否可寫,如果是,則返回 true。 | [ -w $file ] 返回?true。 |
| -x file | 檢測文件是否可執行,如果是,則返回 true。 | [ -x $file ] 返回?true。 |
| -s file | 檢測文件是否為空(文件大小是否大于0),不為空返回 true。 | [ -s $file ] 返回?true。 |
| -e file | 檢測文件(包括目錄)是否存在,如果是,則返回 true。 | [ -e $file ] 返回?true。 |
三、Shell字符串
字符串是shell編程中最常用最有用的數據類型(除了數字和字符串,也沒啥其它類型好用了),字符串可以用單引號,也可以用雙引號,也可以不用引號。單雙引號的區別跟PHP類似。
單引號
<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> 單引號字符串的限制:- 單引號里的任何字符都會原樣輸出,單引號字符串中的變量是無效的;
- 單引號字串中不能出現單引號(對單引號使用轉義符后也不行)。
雙引號
<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> 雙引號的優點:- 雙引號里可以有變量
- 雙引號里可以出現轉義字符
拼接字符串
<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>獲取字符串長度
復制純文本新窗口 <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數組:shell數組的定義、數組長度
Shell在編程方面比Windows批處理強大很多,無論是在循環、運算。
bash支持一維數組(不支持多維數組),并且沒有限定數組的大小。類似與C語言,數組元素的下標由0開始編號。獲取數組中的元素要利用下標,下標可以是整數或算術表達式,其值應大于或等于0。
定義數組
在Shell中,用括號來表示數組,數組元素用“空格”符號分割開。定義數組的一般形式為:? ??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>
還可以單獨定義數組的各個分量: <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> 可以不使用連續的下標,而且下標的范圍沒有限制。
讀取數組
讀取數組元素值的一般格式是:? ??${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> 舉個例子: <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> 運行腳本,輸出: $./test.sh First Index: Zara Second Index: Qadir 使用@ 或 * 可以獲取數組中的所有元素,例如: <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> 舉個例子: <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> 運行腳本,輸出: $./test.sh First Method: Zara Qadir Mahnaz Ayan Daisy Second Method: Zara Qadir Mahnaz Ayan Daisy
獲取數組的長度
獲取數組長度的方法與獲取字符串長度的方法相同,例如: <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);"># 取得數組元素的個數</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);"># 取得數組單個元素的長度</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>總結
以上是生活随笔為你收集整理的Shell 脚本知识回顾 (三) —— 替换、运算符、字符串、数组的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学习笔记(二)JavaScript基本概
- 下一篇: 程序员学好英语的方法(转)