티스토리 뷰

IoT 과정

Array Variable

gaelim 2017. 7. 7. 14:26
반응형

Shell Array

Shell의 Array는 다른 컴파일 언어들( C, C++, Java) 와 다르다. 다음의 예를 보자.

$ arr[0]=red
$ arr[1]=blue
$ arr[5]=green
$ echo ${arr[1]}
blue
$ echo ${arr[*]}            //[2], [3], [4]의 null은 출력하지 않는다.
red blue green
$ echo ${#arr[*]}          //[2], [3], [4]의 갯수는 포함되지 않는다. 
3
$ arr=white                //모든 shell 변수는 variable_name[0] 을 쓰는 것과 같다.
$ echo ${arr[*]}
white blue green
$ echo ${#arr[*]}
3

shell의 Array의 index는 0-1023 까지만 가능하다. 그리고 1차원 배열만 존재한다. 다차원 배열을 지원하지 않는다.


Initializing an Array

Shell Array는 Array initializing을 할 때 존재하는 variable을 unset 시켜주는 것이 중요하다.

unset -v variable_name        # remove the variable if it exists 

index=0
while (( index < 10 ))
do
arr[index]=0
((index+=1))
done
print ${arr[*]}

set -A array v1 v2 v3 v4 ...
-A : Initializes a new variable  // 이 경우 기존에 20 length의 변수가 있다하더라도 10개를 overwrite한다. 근데 기존의 나머지 10개 데이터 또한 날라간다.
array : name for the array
v1-vn : the values for the array, start with index 0
+A : Does not initialize a new variable, but modifies an existing one


Shell Array 특징

array variable은 환경변수로 등록할 수 없다.







반응형

'IoT 과정' 카테고리의 다른 글

Managing Input and Output  (0) 2017.07.07
Functions and Function Libraries  (0) 2017.07.07
Program Loops  (0) 2017.07.07
Shell Patterns  (0) 2017.07.07
Branches and Logic Testing  (0) 2017.07.06