shell陣列(轉)
shell陣列(轉)[@more@]Bash中還可以使用陣列變數,其賦值有兩種:
(1) name = (value1 ... valuen) 此時下標從0開始
(2) name[index] = value
陣列下標的範圍沒有任何限制,同時也不必使用連續的分量.
--------------------------------------------------------------------------------
$ A=(a b c def)
==================================================
$ echo ${A[@]} //取全部元素
a b c def
=================================================
$ echo ${A[0]} //取第一個元素
a
=================================================
//取得陣列元素的個數
$ echo ${#A[@]}
4
$ echo ${#A}
4
$ echo ${#A[3]} //取得元素3的長度
$
==================================================
$ A[3]=yaoshuyin //將第三個元素重新賦值
$ echo ${A[@]}
a b c yaoshuyin
==================================================
//清除變數
$ unset A
$ echo ${A[@]}
$
==================================================
//清空變數,即將值變為空
$ A=
$ echo ${A[@]}
$
==================================================
A=B
B=C
unset $A 事實上所取消的變數是 B 而不是 A
=======================示例 while迴圈========================
#建立陣列
arrSource=("arrJobs.php" "arrSubHangye.php" "arrFirst.php" )
arrDest=("buildhr"
"buildtrain/htdocs"
"bankhr"
"healthr"
"elehr"
)
#取陣列無元素個數
lenArrSource=${#arrSource }
lenArrDest=${#arrDest }
#迴圈列出陣列元素
i=0
while [ $i -lt $lenArrSource ]
do
echo ${arrSource[$i]}
let i++
done
i=0
while [ $i -lt $lenArrDest ]
do
echo ${arrDest[$i]}
let i++
done
=======================示例: for迴圈===============================
#原始檔
arrSource=("/home/800hr/htdocs/login_jump.php")
#目標網站
arrDest=(ithr elehr buildhr bankhr healthr ctvhr chenhr mechr clothr cneduhr 56hr tourhr foodhr greenhr cnlawhr waimaohr)
for outer in ${arrSource } #${arrSource
} 是陣列中的所有元素
do
for inner in ${arrDest }
do
echo "ln -s $outer /home/${inner}/campus/"
done
done
(1) name = (value1 ... valuen) 此時下標從0開始
(2) name[index] = value
陣列下標的範圍沒有任何限制,同時也不必使用連續的分量.
--------------------------------------------------------------------------------
$ A=(a b c def)
==================================================
$ echo ${A[@]} //取全部元素
a b c def
=================================================
$ echo ${A[0]} //取第一個元素
a
=================================================
//取得陣列元素的個數
$ echo ${#A[@]}
4
$ echo ${#A
4
$ echo ${#A[3]} //取得元素3的長度
$
==================================================
$ A[3]=yaoshuyin //將第三個元素重新賦值
$ echo ${A[@]}
a b c yaoshuyin
==================================================
//清除變數
$ unset A
$ echo ${A[@]}
$
==================================================
//清空變數,即將值變為空
$ A=
$ echo ${A[@]}
$
==================================================
A=B
B=C
unset $A 事實上所取消的變數是 B 而不是 A
=======================示例 while迴圈========================
#建立陣列
arrSource=("arrJobs.php" "arrSubHangye.php" "arrFirst.php" )
arrDest=("buildhr"
"buildtrain/htdocs"
"bankhr"
"healthr"
"elehr"
)
#取陣列無元素個數
lenArrSource=${#arrSource
lenArrDest=${#arrDest
#迴圈列出陣列元素
i=0
while [ $i -lt $lenArrSource ]
do
echo ${arrSource[$i]}
let i++
done
i=0
while [ $i -lt $lenArrDest ]
do
echo ${arrDest[$i]}
let i++
done
=======================示例: for迴圈===============================
#原始檔
arrSource=("/home/800hr/htdocs/login_jump.php")
#目標網站
arrDest=(ithr elehr buildhr bankhr healthr ctvhr chenhr mechr clothr cneduhr 56hr tourhr foodhr greenhr cnlawhr waimaohr)
for outer in ${arrSource
do
for inner in ${arrDest
do
echo "ln -s $outer /home/${inner}/campus/"
done
done
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10617731/viewspace-962004/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- shell指令碼(6)-shell陣列指令碼陣列
- linux Shell 命令列-03-array Shell 陣列Linux命令列陣列
- Shell 陣列介紹陣列
- iOS 字典轉陣列,陣列轉字典iOS陣列
- shell切分字串到陣列字串陣列
- PHP xml 轉陣列 陣列轉 xml 操作PHPXML陣列
- js將陣列轉字元,字元轉回陣列JS陣列字元
- shell--函式與陣列函式陣列
- PHP陣列轉換為js陣列PHP陣列JS
- 輪轉陣列陣列
- linux shell陣列深入學習理解Linux陣列
- shell 使用陣列及字串擷取陣列字串
- PHP 陣列轉樹結構/樹結構轉陣列PHP陣列
- js 一維陣列轉二維陣列JS陣列
- js 二維陣列轉一維陣列JS陣列
- 二維陣列和稀疏陣列互轉陣列
- 翻轉int陣列陣列
- js陣列反轉JS陣列
- 【Java】int[] 陣列 和 Integer陣列的轉換Java陣列
- 將字串陣列轉換為浮點數陣列字串陣列
- LeetCode 189 旋轉陣列LeetCode陣列
- vue js 物件轉陣列VueJS物件陣列
- Shell自學二(引數傳遞和陣列)陣列
- Bash Shell指令碼中的陣列使用例項指令碼陣列
- json字串 轉換為陣列JSON字串陣列
- 【LeetCode】189. 旋轉陣列LeetCode陣列
- 玩轉陣列各種方法陣列
- JavaScript 字串轉換為陣列JavaScript字串陣列
- PHP 物件轉換成陣列PHP物件陣列
- 陣列轉換成List集合陣列
- List 和 陣列 的轉換陣列
- LeetCode-189-旋轉陣列LeetCode陣列
- Java中List陣列互轉Java陣列
- LC 189. 旋轉陣列陣列
- 多維陣列轉一維陣列(降維的多種方式)陣列
- shell指令碼字串和陣列的使用操作方法指令碼字串陣列
- 面試最常問的陣列轉樹,樹轉陣列 c++ web框架paozhu實現面試陣列C++Web框架
- string與char陣列相互轉換陣列
- 將一個Collection轉為陣列陣列