'Using yaml in bash script
I have following yaml file and I need to take inputs from this yaml file in my bash script
Database: backup
Table: mytable
Partitions: P10,P11,P12
I tried this like below but getting error
#!/bin/bash
Database=yq e '.Database' t_partitions.yaml
Table=yq e '.Table' t_partitions.yaml
Partitions=yq e '.Partitions' t_partitions.yaml
mysql -u root -p -e "
use $Database;
alter table $Table truncate partition $Partitions;
"
The error is
bash m.sh run
m.sh: line 2: e: command not found
m.sh: line 3: e: command not found
m.sh: line 4: e: command not found
Solution 1:[1]
Your assignment statement is wrong with Bash's grammar.
You need command substitution, like:
#!/bin/bash
Database="$(yq e '.Database' t_partitions.yaml)"
Table="$(yq e '.Table' t_partitions.yaml)"
Partitions="$(yq e '.Partitions' t_partitions.yaml)"
mysql -u root -p -e "
use $Database;
alter table $Table truncate partition $Partitions;
"
Using $() to get output of a command. Use "" to prevent eventually sentence break inside the output by some special character.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Geno Chen |
