简体   繁体   中英

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}

and $2 is the argument of second Xargs and its gets substituted like

helm delete -n ?? {$2}

but i also want $1 to use like this in last comand

helm delete -n {$1} {$2}

is this possible?

output of first xargs

name1
name2
name3

second 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:

.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM