티스토리 뷰

반응형

1. 사용한 파일

  • 테스트를 위한 파일은 AWS의 리전과 코드 목록을 담은 regions.txt 파일이며, 내용은 다음과 같다.
af-south-1
ap-east-1
ap-northeast-1
ap-northeast-2
ap-northeast-3
ap-south-1
ap-southeast-1
ap-southeast-2
ca-central-1
cn-north-1
cn-northwest-1
eu-central-1
eu-north-1
eu-south-1
eu-west-1
eu-west-2
eu-west-3
me-south-1
sa-east-1
us-east-1
us-east-2
us-west-1
us-west-2
@
(CapeTown)
(HongKong)
(Singapore)
(Seoul)
(Osaka-Local)
(Mumbai)
(Singapore)
(Sydney)
(Central)
(Beijing)
(Ningxia)
(Frankfurt)
(Stockholm)
(Milan)
(Ireland)
(London)
(Paris)
(Bahrain)
(SãoPaulo)
(N.Virginia)
(Ohio)
(N.California)
(Oregon)
  • 한 파일로 테스트하기 위해 @를 구분자로 두었음!

 

2. shell script 본문

#!/bin/bash

CURRENTPATH=$( pwd )

TEMP=$( cat $CURRENTPATH/regions.txt )

OIFS=$IFS
IFS='@'

# 요기서 배열로 바뀜
TEMP=($TEMP)
IFS=$OIFS

# @를 기준으로 앞과 뒤를 구분
# TEMP[0]: af-south-1 ap-east-1 ap-northeast-1 ap-northeast-2 ...
CODES=${TEMP[0]}
# TEMP[1]: (CapeTown) (HongKong) (Singapore) (Seoul) ...
REGIONS=${TEMP[1]}

# 공백을 기준으로 string을 배열로 변환
read -a CODES <<< ${CODES}
read -a REGIONS <<< ${REGIONS}

for NUM in "${!CODES[@]}"; do
  echo "${CODES[NUM]}"
  echo "${REGIONS[NUM]}"
done;
  • chmod +x로 실행권한을 준 후에 실행 :D

 

3. 참고

  • 내용이 a, b, c, d, e... 하는 식이라면 다음으로 쉽게 활용이 가능하다.
#!/bin/bash


string="a, b, c, d, e, f, g"

OIFS=$IFS
IFS=', ' read -r -a array <<< "$string"

for NUM in "${!array[@]}"; do
  echo "${array[NUM]}"
done;


# 실행 결과 : 
[~] ./stringToArray.sh                                                                                                                                                              
a
b
c
d
e
f
g

 

댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함