[英]How can capture args with xargs across multiple pipe in bash
I have this command我有这个命令
kubectl get ns -l app=sample-api | awk '/fast/{print $1}' |
xargs -I {} helm list --short -n {} | xargs -I {} helm delete -n ?? {}
Suppose that argument from first xargs is $1 and it gets subsituted in like helm list --short -n {$1}
假设第一个 xargs 的参数是 $1 并且它被替换为类似
helm list --short -n {$1}
and $2 is the argument of second Xargs and its gets substituted like $2 是第二个 Xargs 的参数,它被替换为
helm delete -n ?? {$2}
but i also want $1 to use like this in last comand但我也希望在最后一个命令中像这样使用 $1
helm delete -n {$1} {$2}
is this possible?这可能吗?
output of first xargs第一个 xargs 的 output
name1
name2
name3
second xargs第二个 xargs
chart1
chart2
chart3
I would simply break it up into a separate loop.我会简单地将它分解成一个单独的循环。
kubectl get ns -l app=sample-api |
awk '/fast/{print $1}' |
while read -r name; do
target=$(helm list --short -n "$name")
helm delete -n "$name" "$target"
done
As discussed in comments, if you need this in a Makefile, you'll need to indent every line with a tab, double all the dollar signs, add semicolons between statements, and backslash-escape the internal newlines:如评论中所述,如果您在 Makefile 中需要此功能,则需要使用制表符缩进每一行,将所有美元符号加倍,在语句之间添加分号,并反斜杠转义内部换行符:
.PHONY: kill_em_all
kill_em_all:
kubectl get ns -l app=sample-api |\
awk '/fast/{print $$1}' |\
while read -r name; do\
target=$$(helm list --short -n "$$name");\
helm delete -n "$$name" "$$target";\
done
Unfortunately, Stack Overflow still renders tabs as spaces, so you will not be able to copy/paste this into a Makefile
directly.不幸的是,堆栈溢出仍将制表符呈现为空格,因此您将无法直接将其复制/粘贴到
Makefile
中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.