Kamesh S S
Kamesh S S

Reputation: 15

Bash - execute something if a value is present in yaml file

file.yaml contains

thatyamlkey:
  - key1
  - key2
  - key3

I would like to check if "key2" exists in "thatyamlkey:". If it does, execute something, if not exit 1.

How would I go about this in bash.

This file is accessible via a linux like /path/to/file.yaml or a http://path/to/file.yaml

Spark an idea, please :)

Thanks!

Upvotes: 0

Views: 730

Answers (1)

chepner
chepner

Reputation: 531798

Use a tool like yq (which is a YAML parser wrapped around jq):

filter='.thatyamlkey | any(. == $ARGS.positional[0])'
yq -e "$filter" file.yaml --args key2 > /dev/null || exit 1

...

yq does not make HTTP requests; you'll have to use a tool like curl to fetch a URL first.

Upvotes: 2

Related Questions