0

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
2
  • What do you mean by "getting error"? Commented Apr 7, 2022 at 7:46
  • Database=$(yq e ...) etc... Commented Apr 7, 2022 at 7:50

1 Answer 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.