この記事は約15分6秒で読むことができます。

bashの配列であそんだ

参考文献

配列の初期化

コード表示

[oracle@centos ~]$ echo {a..c} | sed -e 's/ /\n/g'
a
b
c
[oracle@centos ~]$ while read line;do echo ${line}; done < <(echo {a..c} | sed -e 's/ /\n/g')
a
b
c
[oracle@centos ~]$ ary=();while read line;do ary+=(${line}); done < <(echo {a..c} | sed -e 's/ /\n/g')
[oracle@centos ~]$ echo ${ary[@]}
a b c
[oracle@centos ~]$ echo ${#ary[@]}
3
[oracle@centos ~]$ ary=($(echo {a..c} | sed -e 's/ /\n/g'))
[oracle@centos ~]$ echo ${#ary[@]}
1
[oracle@centos ~]$ echo ${ary[@]}
a
b
c
[oracle@centos ~]$ ary=($(echo {a..c}))
[oracle@centos ~]$ echo ${#ary[@]}
1
[oracle@centos ~]$ echo ${ary[@]}
a b c

配列のコピー

こんなかんじ

コード表示

[oracle@centos ~]$ ary=();while read line;do ary+=(${line}); done < <(echo {a..c} | sed -e 's/ /\n/g')
[oracle@centos ~]$ echo ${ary[@]}
a b c
[oracle@centos ~]$ echo ${#ary[@]}
3
[oracle@centos ~]$ aryy=(${ary[@]})
[oracle@centos ~]$ echo ${aryy[@]}
a b c
[oracle@centos ~]$ echo ${#aryy[@]}
3

配列参照するときインデックス気にしなくてイイ

コード表示

[oracle@centos ~]$ ary=($(echo {a..c}))
[oracle@centos ~]$ for i in ${ary[@]};do echo $i; done
a b c
[oracle@centos ~]$ ary=();while read line;do ary+=(${line}); done < <(echo {a..c} | sed -e 's/ /\n/g')
[oracle@centos ~]$ for i in ${ary[@]};do echo $i; done
a
b
c

配列の添字てんかいできるんだね、インデックス気にしたいならこっちか

コード表示

[oracle@centos ~]$ ary=($(echo {a..c}))
[oracle@centos ~]$ echo ${!ary[@]}
0
[oracle@centos ~]$ ary=();while read line;do ary+=(${line}); done < <(echo {a..c} | sed -e 's/ /\n/g')
[oracle@centos ~]$ echo ${!ary[@]}
0 1 2
[oracle@centos ~]$ for i in ${!ary[@]};do echo $i'-->'${ary[$i]};done
0-->a
1-->b
2-->c
[oracle@centos ~]$ for ((i=0;i<${#ary[@]};i++));do echo $i'-->'${ary[$i]};done
0-->a
1-->b
2-->c

配列の要素追加とか要素削除とか

一括削除、一括生成。コピーとか。

コード表示

[oracle@centos ~]$ ary=();while read line;do ary+=(${line}); done < <(echo {a..c} | sed -e 's/ /\n/g')
[oracle@centos ~]$ for ((i=0;i<${#ary[@]};i++));do echo $i'-->'${ary[$i]};done
0-->a
1-->b
2-->c
[oracle@centos ~]$ while read line;do ary+=(${line}); done < <(echo {a..c} | sed -e 's/ /\n/g')
[oracle@centos ~]$ for ((i=0;i<${#ary[@]};i++));do echo $i'-->'${ary[$i]};done
0-->a
1-->b
2-->c
3-->a
4-->b
5-->c
[oracle@centos ~]$ for ((i=0;i<${#ary[@]};i++));do echo $i'-->'${ary[$i]};done
0-->a
1-->b
2-->c
3-->a
4-->b
5-->c
[oracle@centos ~]$ unset ary[@]
[oracle@centos ~]$ for ((i=0;i<${#ary[@]};i++));do echo $i'-->'${ary[$i]};done
[oracle@centos ~]$ ary=();while read line;do ary+=(${line}); done < <(echo {a..c} | sed -e 's/ /\n/g')
[oracle@centos ~]$ for i in ${!ary[@]};do echo $i'-->'${ary[$i]}; done
0-->a
1-->b
2-->c
[oracle@centos ~]$ unset ary[1]
[oracle@centos ~]$ for i in ${!ary[@]};do echo $i'-->'${ary[$i]}; done
0-->a
2-->c
[oracle@centos ~]$ ary=();while read line;do ary+=(${line}); done < <(echo {a..c} | sed -e 's/ /\n/g')
[oracle@centos ~]$ for i in ${!ary[@]};do echo $i'-->'${ary[$i]}; done
0-->a
1-->b
2-->c
[oracle@centos ~]$ unset ary[0]
[oracle@centos ~]$ for i in ${!ary[@]};do echo $i'-->'${ary[$i]}; done
1-->b
2-->c
[oracle@centos ~]$ ary=();while read line;do ary+=(${line}); done < <(echo {a..c} | sed -e 's/ /\n/g')
[oracle@centos ~]$ for i in ${!ary[@]};do echo $i'-->'${ary[$i]}; done
0-->a
1-->b
2-->c
[oracle@centos ~]$ unset ary[2]
[oracle@centos ~]$ for i in ${!ary[@]};do echo $i'-->'${ary[$i]}; done
0-->a
1-->b
[oracle@centos ~]$ ary=();while read line;do ary+=(${line}); done < <(echo {a..c} | sed -e 's/ /\n/g')
[oracle@centos ~]$ for i in ${!ary[@]};do echo $i'-->'${ary[$i]}; done
0-->a
1-->b
2-->c
[oracle@centos ~]$ unset ary[1]
[oracle@centos ~]$ for i in ${!ary[@]};do echo $i'-->'${ary[$i]}; done
0-->a
2-->c
[oracle@centos ~]$ tmp=(${ary[@]});while read line;do ary=();ary+=(${line}); done < <(echo ${tmp[@]})
[oracle@centos ~]$ for i in ${!ary[@]};do echo $i'-->'${ary[$i]}; done
0-->a
1-->c

配列のスライス的な奴

コード表示

[oracle@centos ~]$ ary=();while read line;do ary+=(${line}); done < <(echo {a..z} | sed -e 's/ /\n/g')
[oracle@centos ~]$ for i in ${!ary[@]};do echo $i'-->'${ary[@]:0:$i}; done
[oracle@centos ~]$ for i in ${!ary[@]};do echo $i+1'-->'${ary[@]:0:$i+1}; done

[oracle@centos ~]$ for i in ${!ary[@]};do echo $(($i+1))'-->'${ary[@]:0:$i+1}; done

[oracle@centos ~]$ for i in ${!ary[@]};do echo $i+1'-->'${ary[@]:-($i+1)}; done

[oracle@centos ~]$ for i in ${!ary[@]};do echo $i+1'-->'${ary[@]: -($i+1)}; done

[oracle@centos ~]$ for i in ${!ary[@]};do echo $(($i+1))'-->'${ary[@]: -($i+1)}; done

[oracle@centos ~]$ for i in ${!ary[@]};do echo $(printf '%02d' $(echo $(($i+1))))'-->'${ary[@]: -($i+1)}; done

[oracle@centos ~]$ for i in ${!ary[@]};do echo $(printf '%02d' $(echo $(($i+1))))'-->'${ary[@]: -($i+1)}; done

[oracle@centos ~]$ for i in ${!ary[@]};do echo $(printf '%02d' $(echo $(($i+1))))'-->'${ary[@]:0:$i+1}; done

[oracle@centos ~]$ for i in ${!ary[@]};do echo $(printf '%02d' $(echo $(($i+1))))'-->'${ary[@]:$i:$i+1}; done

[oracle@centos ~]$ for i in ${!ary[@]};do echo $(printf '%02d' $(echo $(($i+1))))'-->'${ary[@]:$i:1}; done

[oracle@centos ~]$ for i in ${!ary[@]};do echo $(printf '%02d' $(echo $(($i+1))))'-->'${ary[@]:-$i:1}; done

Leave a Reply

Your email address will not be published. Required fields are marked *