在 Bash 中打印列表,然后对每一行运行命令

在 Bash 中打印列表,然后对每一行运行命令

因此我在 Mac Bash 中运行以下命令:

Callums-MacBook-Pro-2:chart melman$ helm list
NAME                    REVISION        UPDATED                         STATUS          CHART                   APP VERSION     NAMESPACE
dozing-sasquatch        1               Fri Sep 13 11:31:40 2019        FAILED          pointsapp-0.1.0         1.0.0           default  
exasperated-boxer       1               Fri Sep 13 11:04:56 2019        FAILED          pointsapp-0.1.0         1.0.0           default  
foolhardy-arachnid      1               Fri Sep 13 10:47:45 2019        FAILED          pointsapp-0.1.0         1.0.0           default  
harping-ant             1               Fri Sep 13 09:44:07 2019        FAILED          pointsapp-1.0.0         1.0.0           default  
jazzed-gecko            1               Fri Sep 13 10:41:47 2019        FAILED          pointsapp-1.0.0         1.0.0           default  
luminous-rattlesnake    1               Fri Sep 13 10:51:44 2019        FAILED          pointsapp-0.1.0         1.0.0           default  
oily-deer               1               Fri Sep 13 10:58:17 2019        DEPLOYED        pointsappv2-0.1.0       1.0             default  
old-greyhound           1               Fri Sep 13 11:27:36 2019        FAILED          pointsapp-0.1.0         1.0.0           default  
plinking-llama          1               Fri Sep 13 11:19:23 2019        FAILED          pointsapp-0.1.0         1.0.0           default  
pugnacious-numbat       1               Fri Sep 13 10:07:04 2019        FAILED          pointsapp-1.0.0         1.0.0           default  
telling-skunk           1               Fri Sep 13 10:05:35 2019        FAILED          pointsapp-1.0.0         1.0.0           default  
torpid-gopher           1               Fri Sep 13 10:46:49 2019        FAILED          pointsapp-0.1.0         1.0.0           default  

我如何选择所有名称并将它们传递给helm delete [name],这可以在单个命令中完成吗?

答案1

假设所有名称都不包含空格,您可以使用循环for来循环遍历列表行,通过管道跳过tail标题行,然后awk仅打印第一列:

for name in $(helm list | tail -n+2 | awk '{print $1}'); do helm delete $name; done

您确实要删除整个列表吗?

E:若要仅删除失败的,请输入grep

for name in $(helm list | tail -n+2 | grep "FAILED" | awk '{print $1}'); do helm delete $name; done

相关内容