Home > Software design >  How can capture args with xargs across multiple pipe in bash
How can capture args with xargs across multiple pipe in bash

Time:09-12

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

CodePudding user response:

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.

  • Related