티스토리 뷰
반응형
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
'Linux.' 카테고리의 다른 글
[Linux] 디렉토리에 포함된 파일 개수 세기 (0) | 2021.01.05 |
---|---|
[Shell script] sed로 특정 문자열이 포함된 라인 치환 (0) | 2020.12.17 |
[Shell script] sed로 첫글자, 마지막 글자 제거하기 (0) | 2020.11.13 |
[Shell script] 명령어 성공 여부에 따른 분기처리 (0) | 2020.11.04 |
[Shell script] IP 주소 배열로 저장하기 (0) | 2020.11.02 |
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- etc
- postgresql
- pgloader
- Puppeteer
- javascript
- Gradle
- AWS IoT
- shell
- mysql
- Spring Cloud Config
- JEST
- Java
- IntelliJ
- Docker
- spring boot
- Vault
- 코딩테스트
- kotlin
- RancherDesktop
- Git
- JPA
- Node.js
- AWS
- eureka
- Linux
- react
- terraform
- hashicorp
- jQuery
- Database
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함