日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Shell 脚本知识回顾 (三) —— 替换、运算符、字符串、数组

發布時間:2023/12/9 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Shell 脚本知识回顾 (三) —— 替换、运算符、字符串、数组 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、Shell替換:Shell變量替換,命令替換,轉義字符

如果表達式中包含特殊字符,Shell 將會進行替換。例如,在雙引號中使用變量就是一種替換,轉義字符也是一種替換。

舉個例子: [cpp]?view plaincopy
  • #!/bin/bash??
  • a=10??
  • echo?-e?"Value?of?a?is?$a?\n"??
  • 運行結果:Value of a is 10這里 -e 表示對轉義字符進行替換。如果不使用 -e 選項,將會原樣輸出:Value of a is 10\n
    下面的轉義字符都可以用在 echo 中: 轉義字符 含義
    \\反斜杠
    \a警報,響鈴
    \b退格(刪除鍵)
    \f換頁(FF),將當前位置移到下頁開頭
    \n換行
    \r回車
    \t水平制表符(tab鍵)?
    \v垂直制表符
    可以使用 echo 命令的 -E 選項禁止轉義,默認也是不轉義的;使用 -n 選項可以禁止插入換行符。

    命令替換

    命令替換是指Shell可以先執行命令,將輸出結果暫時保存,在適當的地方輸出。

    命令替換的語法:
    [cpp]?view plaincopy
  • `command`??
  • 注意是反引號,不是單引號,這個鍵位于 Esc 鍵下方。

    下面的例子中,將命令執行結果保存在變量中:
    [cpp]?view plaincopy
  • #!/bin/bash??
  • DATE=`date`??
  • echo?"Date?is?$DATE"??
  • USERS=`who?|?wc?-l`??
  • echo?"Logged?in?user?are?$USERS"??
  • UP=`date?;?uptime`??
  • echo?"Uptime?is?$UP"??
  • 運行結果:Date is Thu Jul 2 03:59:57 MST 2009 Logged in user are 1 Uptime is Thu Jul 2 03:59:57 MST 2009 03:59:57 up 20 days, 14:03, 1 user, load avg: 0.13, 0.07, 0.15


    變量替換

    變量替換可以根據變量的狀態(是否為空、是否定義等)來改變它的值?可以使用的變量替換形式:
    形式 說明
    ${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
  • Variable?is?not?set??
  • 1?-?Value?of?var?is??
  • Variable?is?not?set??
  • 2?-?Value?of?var?is?Variable?is?not?set??
  • 3?-?Value?of?var?is??
  • This?is?default?value??
  • 4?-?Value?of?var?is?Prefix??
  • Prefix??
  • 5?-?Value?of?var?is?Prefix??

  • 二、Shell運算符:Shell算數運算符、關系運算符、布爾運算符、字符串運算符等

    Bash 支持很多運算符,包括算數運算符、關系運算符、布爾運算符、字符串運算符和文件測試運算符。

    原生bash不支持簡單的數學運算,但是可以通過其他命令來實現,例如 awk 和 expr,expr 最常用。

    expr 是一款表達式計算工具,使用它能完成表達式的求值操作。

    例如,兩個數相加:

    [cpp]?view plaincopy
  • #!/bin/bash??
  • val=`expr?2?+?2`??
  • echo?"Total?value?:?$val"??
  • 運行腳本輸出:

    Total value : 4 兩點注意:
    • 表達式和運算符之間要有空格,例如 2+2 是不對的,必須寫成 2 + 2,這與我們熟悉的大多數編程語言不一樣。
    • 完整的表達式要被 ` ` 包含,注意這個字符不是常用的單引號,在 Esc 鍵下邊。

    算術運算符

    先來看一個使用算術運算符的例子
    [cpp]?view plaincopy
  • <pre?name="code"?class="cpp">#!/bin/sh??
  • a=10??
  • b=20??
  • val=`expr?$a?+?$b`??
  • echo?"a?+?b?:?$val"??
  • val=`expr?$a?-?$b`??
  • echo?"a?-?b?:?$val"??
  • val=`expr?$a?\*?$b`??
  • echo?"a?*?b?:?$val"??
  • val=`expr?$b?/?$a`??
  • echo?"b?/?a?:?$val"??
  • val=`expr?$b?%?$a`??
  • echo?"b?%?a?:?$val"??
  • if?[?$a?==?$b?]??
  • then??
  • ???echo?"a?is?equal?to?b"??
  • fi??
  • if?[?$a?!=?$b?]??
  • then??
  • ???echo?"a?is?not?equal?to?b"??
  • fi??

  • 運行結果: 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
  • #!/bin/sh??
  • a=10??
  • b=20??
  • if?[?$a?-eq?$b?]??
  • then??
  • ???echo?"$a?-eq?$b?:?a?is?equal?to?b"??
  • else??
  • ???echo?"$a?-eq?$b:?a?is?not?equal?to?b"??
  • fi??
  • if?[?$a?-ne?$b?]??
  • then??
  • ???echo?"$a?-ne?$b:?a?is?not?equal?to?b"??
  • else??
  • ???echo?"$a?-ne?$b?:?a?is?equal?to?b"??
  • fi??
  • if?[?$a?-gt?$b?]??
  • then??
  • ???echo?"$a?-gt?$b:?a?is?greater?than?b"??
  • else??
  • ???echo?"$a?-gt?$b:?a?is?not?greater?than?b"??
  • fi??
  • if?[?$a?-lt?$b?]??
  • then??
  • ???echo?"$a?-lt?$b:?a?is?less?than?b"??
  • else??
  • ???echo?"$a?-lt?$b:?a?is?not?less?than?b"??
  • fi??
  • if?[?$a?-ge?$b?]??
  • then??
  • ???echo?"$a?-ge?$b:?a?is?greater?or??equal?to?b"??
  • else??
  • ???echo?"$a?-ge?$b:?a?is?not?greater?or?equal?to?b"??
  • fi??
  • if?[?$a?-le?$b?]??
  • then??
  • ???echo?"$a?-le?$b:?a?is?less?or??equal?to?b"??
  • else??
  • ???echo?"$a?-le?$b:?a?is?not?less?or?equal?to?b"??
  • fi??
  • 運行結果:

    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
  • #!/bin/sh??
  • a=10??
  • b=20??
  • if?[?$a?!=?$b?]??
  • then??
  • ???echo?"$a?!=?$b?:?a?is?not?equal?to?b"??
  • else??
  • ???echo?"$a?!=?$b:?a?is?equal?to?b"??
  • fi??
  • if?[?$a?-lt?100?-a?$b?-gt?15?]??
  • then??
  • ???echo?"$a?-lt?100?-a?$b?-gt?15?:?returns?true"??
  • else??
  • ???echo?"$a?-lt?100?-a?$b?-gt?15?:?returns?false"??
  • fi??
  • if?[?$a?-lt?100?-o?$b?-gt?100?]??
  • then??
  • ???echo?"$a?-lt?100?-o?$b?-gt?100?:?returns?true"??
  • else??
  • ???echo?"$a?-lt?100?-o?$b?-gt?100?:?returns?false"??
  • fi??
  • if?[?$a?-lt?5?-o?$b?-gt?100?]??
  • then??
  • ???echo?"$a?-lt?100?-o?$b?-gt?100?:?returns?true"??
  • else??
  • ???echo?"$a?-lt?100?-o?$b?-gt?100?:?returns?false"??
  • fi??
  • 運行結果: 10 != 20 : a is not equal to b 10 -lt 100 -a 20 -gt 15 : returns true 10 -lt 100 -o 20 -gt 100 : returns true 10 -lt 5 -o 20 -gt 100 : returns false
    布爾運算符列表 運算符 說明 舉例
    !非運算,表達式為 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
  • #!/bin/sh??
  • a="abc"??
  • b="efg"??
  • if?[?$a?=?$b?]??
  • then??
  • ???echo?"$a?=?$b?:?a?is?equal?to?b"??
  • else??
  • ???echo?"$a?=?$b:?a?is?not?equal?to?b"??
  • fi??
  • if?[?$a?!=?$b?]??
  • then??
  • ???echo?"$a?!=?$b?:?a?is?not?equal?to?b"??
  • else??
  • ???echo?"$a?!=?$b:?a?is?equal?to?b"??
  • fi??
  • if?[?-z?$a?]??
  • then??
  • ???echo?"-z?$a?:?string?length?is?zero"??
  • else??
  • ???echo?"-z?$a?:?string?length?is?not?zero"??
  • fi??
  • if?[?-n?$a?]??
  • then??
  • ???echo?"-n?$a?:?string?length?is?not?zero"??
  • else??
  • ???echo?"-n?$a?:?string?length?is?zero"??
  • fi??
  • if?[?$a?]??
  • then??
  • ???echo?"$a?:?string?is?not?empty"??
  • else??
  • ???echo?"$a?:?string?is?empty"??
  • fi??
  • 運行結果:

    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 plaincopy
  • #!/bin/sh??
  • file="/var/www/tutorialspoint/unix/test.sh"??
  • if?[?-r?$file?]??
  • then??
  • ???echo?"File?has?read?access"??
  • else??
  • ???echo?"File?does?not?have?read?access"??
  • fi??
  • if?[?-w?$file?]??
  • then??
  • ???echo?"File?has?write?permission"??
  • else??
  • ???echo?"File?does?not?have?write?permission"??
  • fi??
  • if?[?-x?$file?]??
  • then??
  • ???echo?"File?has?execute?permission"??
  • else??
  • ???echo?"File?does?not?have?execute?permission"??
  • fi??
  • if?[?-f?$file?]??
  • then??
  • ???echo?"File?is?an?ordinary?file"??
  • else??
  • ???echo?"This?is?sepcial?file"??
  • fi??
  • if?[?-d?$file?]??
  • then??
  • ???echo?"File?is?a?directory"??
  • else??
  • ???echo?"This?is?not?a?directory"??
  • fi??
  • if?[?-s?$file?]??
  • then??
  • ???echo?"File?size?is?zero"??
  • else??
  • ???echo?"File?size?is?not?zero"??
  • fi??
  • if?[?-e?$file?]??
  • then??
  • ???echo?"File?exists"??
  • else??
  • ???echo?"File?does?not?exist"??
  • fi??
  • [cpp]?view plaincopy
  • </pre><pre?code_snippet_id="1663121"?snippet_file_name="blog_20160426_11_7216744"?name="code"?class="cpp"?style="background-color:?rgb(255,?255,?255);">運行結果:??
  • File 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 脚本知识回顾 (三) —— 替换、运算符、字符串、数组的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。