我有下面的yaml文件,我需要在我的bash脚本中从这个yaml文件中获取输入
Database: backup
Table: mytable
Partitions: P10,P11,P12
我试过下面这样,但得到错误
#!/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;
"
错误为
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
您的赋值语句与Bash的语法不一致。
你需要命令替换,比如:
#!/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;
"
使用$()
获取命令的输出。使用""
来防止某些特殊字符最终在输出中打断句子。