2019年4月17日 星期三

【LINUX】SHELL中的數組

數組:就是相同數據類型,只用一個變量集合它們

範例 :


定義數組一般以括號的方式來定義
[root@rosalie ~]# A=(test1 test2 test3)  

${A[0]} 指取用單一取用,數字0表示test1、數字1表示test2 、數字2表示test3     
[root@rosalie ~]# echo ${A[0]}
test1

${A[@]} ,顯示所有參數即  test1 test2 test3
${A[@]:0},同上,但若要從第二個數據開始${A[@]:1}
[root@rosalie ~]# echo ${A[@]}
test1 test2 test3

${#A[@]} ,顯示數值個數即數量,3
[root@rosalie ~]# echo ${#A[@]}
4

${A[@]/test2/test5} ,替換表示將test2替換成test5
[root@rosalie ~]# echo ${A[@]/test2/test5}
test1 test5 test3

unset A[2];echo ${A[@]} ,刪除test3數組
[root@rosalie ~]# unset A[2];echo ${A[@]}
test1 test2
[root@rosalie ~]# echo ${A[@]}
test1 test2

A[3]=test3,增加第三組數組及第四組
[root@rosalie ~]# A[3]=test3
[root@rosalie ~]# A[4]=test4

${A[@]:0:3},顯示數組從第一組算起,取三個組數
[root@rosalie ~]# echo ${A[@]:0:3}
test1 test2 test3

${A[@]:(-2):2},顯示數組倒數二組算起,取二個組數,即從後面數二個
[root@rosalie ~]# echo ${A[@]:(-2):2}
test3 test4

沒有留言:

張貼留言

【MYSQL】MYSQL的SYS表說明(版本8.0)

mysql> use sys Reading table information for completion of table and column names You can turn off this feature to get a quicker s...