在提到this question时,我仍然挣扎于下面的单线行来工作。
kubectl get ns | while read -r line; do echo `$line | grep Term | cut -d' ' -f1`; done我希望打印出$line | grep Term | cut -d' ' -f1的结果,但是它打印出一个空行(echo),然后执行$line | grep Term | cut -d' ' -f1的结果
bash: NAME: command not found
bash: cert-manager: command not found
bash: configmap-4262: command not found
bash: configmap-4430: command not found同样的结果与略有不同的方法相同:
kubectl get ns | while read -r line; do ns=`$line | grep Term | cut -d' ' -f1`; echo $ns; done我真正想要实现的是使用$line | grep Term | cut -d'的结果作为shell脚本的输入。
do ns=`$line | grep Term | cut -d' ' -f1`; ./delete-kube-ns.sh $ns;或
$line | grep Term | cut -d' ' -f1` | xargs ./delete-kube-ns.sh发布于 2020-02-19 15:17:16
有了你的建议,我实际上可以实现我想要做的事情。
kubectl get ns | awk '/Term/ { print $1 }' | while IFS= read -r line; do
~/bin/kill-kube-ns.sh $line
done
Killed namespace: configmap-4500
Killed namespace: configmap-5062
Killed namespace: configmap-5526
Killed namespace: configmap-5817
Killed namespace: configmap-6143发布于 2020-02-14 16:39:33
把后背移开。它试图以shell命令的形式执行kubectl的输出。您希望回显$line,而不是将其作为命令执行的结果。
kubectl get ns | while read -r line; do
echo "$line" | grep Term | cut -d' ' -f1
done似乎根本没有必要使用while read,只需将kubectl输送到grep
kubectl get ns | grep Term | cut -d' ' -f1发布于 2020-02-15 09:49:29
parameter expansion可能是您想要/需要的,而不是将grep和cut嵌入到while read循环中。
#!/usr/bin/env bash
kubectl get ns | while IFS= read -r line; do
[[ $line == Term ]] && ns=${line% *}
echo "$ns"
done根据模式匹配,您可能需要*Term*
https://stackoverflow.com/questions/60230242
复制相似问题