Home > front end >  Using yaml in bash script
Using yaml in bash script

Time:04-07

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

CodePudding user response:

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.

  • Related