【发布时间】:2019-12-17 20:36:47
【问题描述】:
此代码在 AWK 之外按预期工作
password_cmd="kubectl get secrets -o jsonpath=\'{.data.ssl-store-password}\' -n $namespace $password_secret"
echo $password_cmd
password=eval $password_cmd
输出:
kubectl get secrets -o jsonpath=\'{.data.ssl-store-password}\' -n aircourier secret-aircourier-mq-password
在 AWK 中使用它时,我在转义引号时遇到问题
第一次尝试
password_cmd="kubectl get secrets -o jsonpath=\'\''{.data.ssl-store-password}\'\'' -n $namespace $password_secret";
输出
awk: cmd. line:9: warning: escape sequence `\'' treated as plain `''
第二次尝试
password_cmd='\''kubectl get secrets -o jsonpath=\'\''{.data.ssl-store-password}\'\'' -n $namespace $password_secret'\'';
输出
awk: cmd. line:9: password_cmd='kubectl get secrets -o jsonpath=\'{.data.ssl-store-password}\' -n $namespace $password_secret';
awk: cmd. line:9: ^ invalid char ''' in expression
到目前为止的完整脚本
#!/bin/bash
kubectl get secrets --all-namespaces | grep jks > keystores.tmp
# create table headers in the file for manage engine
echo '<--table K8_jks_secrets starts-->' > table.out
echo 'Namespace , Secret Name, Expire Date, Days Remaining' >> table.out
# awk through each line in keystores.tmp that we created earier
awk '{
#print $1, ", " $2, ", ";
namespace=1;
jks_secret=2;
print $namespace, ", " $jks_secret, ", ";
$password_secret=substr($2,1,length($2)-3)"password";
print $password_secret
password_cmd='\"'kubectl get secrets -o jsonpath=\'\''{.data.ssl-store-password}\'\'' -n $namespace $password_secret'\"';
print $password_cmd ;
}' keystores.tmp >> table.out
echo '<--table K8_jks_secrets starts-->' >> table.out
【问题讨论】:
-
JSON 片段周围的文字单引号几乎可以肯定是一个错误。
-
您的实际 Awk 代码在哪里?
-
shell 变量肯定不会被 Awk 扩展。 You should properly quote them in the shell,但如果这是有代表性的,您的 awk 脚本可能包含更多错误。
-
namespace=1;然后... $namespace可能无法按您的预期工作。只需在 awk 中进行测试,即。awk 'END{namespace=1; print "ns=" $namespace}' /dev/null。祝你好运。 -
另一个
awk测试是echo "one two" | awk '{namespace=1; print "ns=" $namespace}'。祝你好运。