티스토리 뷰
반응형
yq 간단 사용법 - 값 확인
yq
yq is a lightweight and portable command-line YAML processor
mikefarah.gitbook.io
- json의 jq처럼 yaml을 파싱하거나 수정해줄 수 있게 함.
- 형식: yq [명령어] [옵션] '.키 = "값"' 파일명.yml
- 따옴표, 쌍따옴표는 빠트리면 안됨! 이것때문에 애를 많이 먹었다.
- 예시:
# ingnoh.yml
test: yq
ingnoh:
age: 3
whoami: monkey
hello:
world: yes
- 위 예시 파일로부터 특정 키 값을 확인해볼 수 있다.
[~] yq eval '.test' ingnoh.yml
yq
[~] # eval은 e로 줄여 쓸 수 있다.
[~] yq e '.hello.world' ingnoh.yml
yes
yq 간단 사용법 - 값 추가(또는 수정)
- 값을 추가할 수 있으며, 수정된 값은 stdout으로 출력된다.
- 이미 있는 키의 값을 수정하고자 하는 경우에도 사용이 가능하다.
- 수정된 yml 파일을 사용하고자 하는 경우 redirection 또는 --inplace 옵션을 사용할 수 있다.
[~] # 원본 파일 확인
[~] cat ingnoh.yml
test: yq
ingnoh:
age: 3
whoami: monkey
hello:
world: yes
[~] # .test의 값을 no로 변경하여 stdout에 출력
[~] yq e '.test = "no"' ingnoh.yml
test: no # 변경된 것으로 보임
ingnoh:
age: 3
whoami: monkey
hello:
world: yes
[~] # 그러나 원본은 변경되지 않았음!
[~] cat ingnoh.yml
test: yq # 변경되지 않음
ingnoh:
age: 3
whoami: monkey
hello:
world: yes
[~] # 실제로 원본을 변경하고자 하는 경우, redirection 또는 --inplace 옵션을 사용
[~] yq e --inplace '.test = "no"' ingnoh.yml
[~] # 변경 확인
[~] cat ingnoh.yml
test: no # 변경되었음
ingnoh:
age: 3
whoami: monkey
hello:
world: yes
변수 사용
- yq e(=eval의 약어)에서 특정 값을 변수를 통해 변경하려면 다음과 같이 env()로 사용한다.
[~] # HOME 환경 변수 확인
[~] echo $HOME
/Users/ingnoh
[~] # 환경 변수로 yml 파일 변경을 시도
[~] yq e '.test = "$HOME"' ingnoh.yml
test: $HOME # 원하는대로 적용되지 않음
ingnoh:
age: 3
whoami: monkey
hello:
world: yes
[~] # 변수명="${사용할 변수}" yq e '.키 = env(변수명)' 파일.yml
[~] home="${HOME}" yq e '.test = env(home)' ingnoh.yml
test: /Users/ingnoh # 원하는대로 적용되었음
ingnoh:
age: 3
whoami: monkey
hello:
world: yes
- 키를 변경하려면 .[env(변수명)] 형태로 사용한다.
[~] # 의도된대로 동작하지 않음
[~] yq e '.$HOME = "abc"' ingnoh.yml
test: no
ingnoh:
age: 3
whoami: monkey
hello:
world: yes
$HOME: abc # 원하는 형태로 적용되지 않음
[~] # env(변수명) 형태로 사용할 경우 동작하지 않음
[~] home="${HOME}" yq e 'env(home) = "abc"' ingnoh.yml
test: no
ingnoh:
age: 3
whoami: monkey
hello:
world: yes
[~] # 온점(.)을 넣으면 에러가 발생!
[~] home="${HOME}" yq e '.env(home) = "abc"' ingnoh.yml
Error: Parsing expression: Lexer error: could not match text starting at 1:6 failing at 1:8.
unmatched text: "ho"
[~] # 올바른 형태 : 사용할변수명="${변수}" yq e '.[env(사용할변수명)] = "매핑할 값"' 파일.yml
[~] home="${HOME}" yq e '.[env(home)] = "abc"' ingnoh.yml
test: no
ingnoh:
age: 3
whoami: monkey
hello:
world: yes
/Users/ingnoh: abc # 적용되었음
사용 예시
- .config.txt에 설정 값을 받고, 이를 키 / 값 형태의 map으로 바꾸어 spring config yml의 설정 값을 shell script에서 바꾸고자 하였음.
- .config.txt
ENV=prod
AWS_REGION=ap-northeast-2
EUREKA_REGISTER=true
EUREKA_INSTANCE_IP=127.0.0.1
EUREKA_DEFAULT_ZONE=http://ingnoh.tistory.com/eureka
- spring config yml 파일
# spring config
spring:
profiles: dev
# aws config
aws:
region: us-west-1
# eureka config
eureka:
instance:
ipAddress: 0.0.0.0
client:
serviceUrl:
defaultZone: abc
register-with-eureka: false
- 다음과 같이 yq를 활용
#!/bin/bash
source ".configs.txt"
# props를 dictionary 형태로 사용
declare -A props=( [".spring.profiles"]="${ENV}" [".aws.region"]="${AWS_REGION}" [".eureka.instance.ipAddress"]="${EUREKA_INSTANCE_IP}" [".eureka.client.serviceUrl.defaultZone"]="${EUREKA_DEFAULT_ZONE}" [".eureka.client.register-with-eureka"]="${EUREKA_REGISTER}" )
for idx in "${!props[@]}"; do
# props_param은 .spring.profiles = "prod" 형태로 문자열을 저장
props_param="${idx} = \"${props[${idx}]}\""
# --inplace 옵션은 yq를 통해 읽어들인 파일에 eval 결과를 그대로 저장
eval "yq e --inplace '${props_param}' spring-config-dev.yml"
done
- eval이 없으면 에러(unmatched text double quotes: "'")가 자꾸 발생함... 원인을 찾지 못하여 eval을 사용하였음.
'Linux.' 카테고리의 다른 글
[dd] 1GB 파일 만들기 (0) | 2022.06.08 |
---|---|
[Shell script] 프로그램의 설치여부 확인 (0) | 2021.04.30 |
[Linux] Nohup이란? (0) | 2021.02.15 |
[SSH] Permission denied(public key) 원인 및 해결 (0) | 2021.01.08 |
[Linux] 디렉토리에 포함된 파일 개수 세기 (0) | 2021.01.05 |
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- javascript
- Docker
- Node.js
- eureka
- jQuery
- terraform
- Vault
- Puppeteer
- Java
- mysql
- IntelliJ
- Spring Cloud Config
- JEST
- pgloader
- Gradle
- etc
- hashicorp
- spring boot
- react
- AWS
- postgresql
- Linux
- Git
- JPA
- 코딩테스트
- shell
- kotlin
- RancherDesktop
- Database
- AWS IoT
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함