當前位置:
首頁 >
Bash 数组示例
發布時間:2025/3/20
26
豆豆
原文 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 Suse4 獲取數組長度
語法
${#arrayname[@]}
$ cat arraymanip.sh declare -a Unix=('Debian' 'Red hat' 'Suse' 'Fedora'); echo ${#Unix[@]} # 數組長度 echo ${#Unix} # 這個是獲取數組第一個元素字符數量 .i.e Debian $./arraymanip.sh 4 65 數組中第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 46 通過數組的偏移量和長度來提取子數組
下面的例子從數組Unix 位置3開始,提取2個元素, 索引從0開始
$cat arraymanip.sh Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux'); echo ${Unix[@]:3:2}$./arraymanip.sh Suse Fedora7 根據偏移和長度獲取數組元素的一部分
例如,位于數組的第二個索引的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 AIX10 在從數組刪除元素
使用 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 OpenLinux13 合并兩個數組
展開兩個數組的元素并將其分配給新數組
$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 1414 刪除整個數組
使用 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 015 從文件加載數組
讀取文件每一行,一行作為數組中的一個元素
#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年技術見證,附贈技術全景圖總結
- 上一篇: 确定一组矩形是否有两个重叠的算法
- 下一篇: Vert.x中EventBus中的使用