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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Bash 数组示例

發布時間:2025/3/20 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Bash 数组示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文 http://www.thegeekstuff.com/2...

1 數組聲明

像下面 會自動創建 name 數組,不用聲明

name[index]=value

例如

$ cat arraymanip.sh #! /bin/bash Unix[0]='Debian' Unix[1]='Red hat' Unix[2]='Ubuntu' Unix[3]='Suse'echo ${Unix[1]}$./arraymanip.sh Red hat

通過索引訪問元素 ${name[index]}

2 聲明時初始化數組

語法

Syntax: declare -a arrayname=(element1 element2 element3)

元素使用空格分隔

#! /bin/bash $cat arraymanip.sh declare -a Unix=('Debian' 'Red hat' 'Red hat' 'Suse' 'Fedora');

3 打印整個數組

如果索引是 @ 或 * 表示整個數組

echo ${Unix[@]}# Add the above echo statement into the arraymanip.sh #./t.sh Debian Red hat Ubuntu Suse

4 獲取數組長度

語法

${#arrayname[@]}

$ cat arraymanip.sh declare -a Unix=('Debian' 'Red hat' 'Suse' 'Fedora'); echo ${#Unix[@]} # 數組長度 echo ${#Unix} # 這個是獲取數組第一個元素字符數量 .i.e Debian $./arraymanip.sh 4 6

5 數組中第n個元素長度

${#arrayname[n]}

$cat arraymanip.sh #! /bin/bashUnix[0]='Debian' Unix[1]='Red hat' Unix[2]='Ubuntu' Unix[3]='Suse'echo ${#Unix[3]} # length of the element located at index 3 i.e Suse$./arraymanip.sh 4

6 通過數組的偏移量和長度來提取子數組

下面的例子從數組Unix 位置3開始,提取2個元素, 索引從0開始

$cat arraymanip.sh Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux'); echo ${Unix[@]:3:2}$./arraymanip.sh Suse Fedora

7 根據偏移和長度獲取數組元素的一部分

例如,位于數組的第二個索引的Ubuntu 獲取基 第0個到第4個字符

$cat arraymanip.sh #! /bin/bashUnix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux'); echo ${Unix[2]:0:4}./arraymanip.sh Ubun

上述示例從數組的第二個索引元素中提取前四個字符

8 搜索 替換 數組中元素

下面的代碼 搜索 Ubuntu 然后 替換成 SCO Unix

$cat arraymanip.sh #!/bin/bash Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');echo ${Unix[@]/Ubuntu/SCO Unix} echo ${Unix[@]}$./arraymanip.sh Debian Red hat SCO Unix Suse Fedora UTS OpenLinux Debian Red hat Ubuntu Suse Fedora UTS OpenLinux

在這個例子中, 它用“SCO Unix”替換了第二個索引“Ubuntu”中的元素, 但是這個例子不會改變原來數組

9 向數組中添加元素

向數組Unix中添加 AIX HP-UX

$cat arraymanip.sh Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux'); Unix=("${Unix[@]}" "AIX" "HP-UX") echo ${Unix[7]}$./arraymanip.sh AIX

10 在從數組刪除元素

使用 unset 刪除索引3的Suse

$cat arraymanip.sh #!/bin/bash Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');unset Unix[3] echo ${Unix[3]}

上面的例子打印索引3的值為空,并不是我們想要的結果, 下面的例子完整的移除一個元素

$ cat arraymanip.sh Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux'); pos=3 Unix=(${Unix[@]:0:$pos} ${Unix[@]:$(($pos + 1))}) echo ${Unix[@]}$./arraymanip.sh Debian Red hat Ubuntu Fedora UTS OpenLinux

上面的代碼中 ${Unix[@]:0:$pos} 表示 從索引0到pos的子數組 ${Unix[@]:4} 表示從索引4到最后一個元素的子數組
然后 把兩個子數組合并成一個數組

11 通過模式刪除數組元素

在搜索條件下,您可以給出模式,并將剩余元素存儲到另一個數組,比如

$ cat arraymanip.sh #!/bin/bash declare -a Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora'); declare -a patter=( ${Unix[@]/Red*/} ) echo ${patter[@]}$ ./arraymanip.sh Debian Ubuntu Suse Fedora

上述示例刪除了具有模式Red* 的元素

12 復制一個數組

展開數組元素并將其存儲到新的數組中

#!/bin/bash Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux'); Linux=("${Unix[@]}") echo ${Linux[@]}$ ./arraymanip.sh Debian Red hat Ubuntu Fedora UTS OpenLinux

13 合并兩個數組

展開兩個數組的元素并將其分配給新數組

$cat arraymanip.sh #!/bin/bash Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux'); Shell=('bash' 'csh' 'jsh' 'rsh' 'ksh' 'rc' 'tcsh');UnixShell=("${Unix[@]}" "${Shell[@]}") echo ${UnixShell[@]} echo ${#UnixShell[@]}$ ./arraymanip.sh Debian Red hat Ubuntu Suse Fedora UTS OpenLinux bash csh jsh rsh ksh rc tcsh 14

14 刪除整個數組

使用 unset 命令

$cat arraymanip.sh #!/bin/bash Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux'); Shell=('bash' 'csh' 'jsh' 'rsh' 'ksh' 'rc' 'tcsh');UnixShell=("${Unix[@]}" "${Shell[@]}") unset UnixShell echo ${#UnixShell[@]}$ ./arraymanip.sh 0

15 從文件加載數組

讀取文件每一行,一行作為數組中的一個元素

#Example file $ cat logfile Welcome to thegeekstuff Linux Unix$ cat loadcontent.sh #!/bin/bash filecontent=( `cat "logfile" `)for t in "${filecontent[@]}" doecho $t done echo "Read file content!"$ ./loadcontent.sh Welcome to thegeekstuff Linux Unix Read file content! 與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的Bash 数组示例的全部內容,希望文章能夠幫你解決所遇到的問題。

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