我试图在一行中完成以下任务:
<command> | xargs -I {} grep {} <(other command)>
我有一个 Kubernetes 集群,正在运行 Redis 和许多其他应用程序。我想找出哪些应用程序(在本例中为 Pod)连接到 Redis。
这可以分为两个步骤。
- 获取连接到 Redis 的 IP 列表。
- 获取保存这些 IP 的 Pod 名称。
我使用以下命令获取了 IP 列表:
$ kubectl exec -it redis -- redis-cli -a <redis-auth> client list | tail -n +2 | awk -F '[:= ]' '{ print $4 }' | sort | uniq > test.txt
$ cat test.txt
10.52.1.194
10.52.1.91
10.52.2.44
10.52.3.223
127.0.0.1
我还可以使用以下命令获取 pod 名称:
$ grep -f test.txt <(kubectl get pods -o wide)
app1 1/1 Running 0 9d 10.52.1.91 node-8 <none> <none>
app2 2/2 Running 0 79d 10.52.2.44 node-14 <none> <none>
app3 2/2 Running 2 79d 10.52.1.194 node-11 <none> <none>
app4 1/1 Running 0 5d10h 10.52.3.223 node-30 <none> <none>
我想在一行中完成这两件事。我尝试了以下方法:
$ kubectl exec -it redis -- redis-cli -a <redis-auth> client list | tail -n +2 | awk -F '[:= ]' '{ print $4 }' sort | uniq | xargs -t -I{} grep {} <(kubectl get pods -o wide)
它打印以下命令(应该执行),但只执行第一个命令:
grep 10.52.1.194 /dev/fd/63
app3 2/2 Running 2 79d 10.52.1.194 node-11 <none> <none>
grep 127.0.0.1 /dev/fd/63
grep 10.52.1.91 /dev/fd/63
grep 10.52.2.44 /dev/fd/63
grep 10.52.3.223 /dev/fd/63
我怎样才能在一行中完成这个任务。另外,我知道我可以为连接到 Redis 的每个客户端提供一个名称
,但这需要更改应用程序代码,而我现在无法执行此操作。
编辑:
我给出的单行实际上部分起作用,因为它只执行第一个 grep ...
而不是所有 grep ...
命令。谁能解释一下我在这里做错了什么?
编辑2:
我的假设是,只有第一个 grep 命令有效,在第一个 grep 命令之后,/dev/fd/63
可能指向一个不存在或空文件。另外,以下命令由于某种原因起作用。谁能解释一下吗?
$ kubectl exec -t redis -- redis-cli -a <redis-auth> client list | tail -n +2 | awk -F '[:= ]' '{ print $4 }' | sort -u | xargs -t -I{} bash -c 'grep -w {} <(kubectl get pods -o wide)'
bash -c grep -w 10.52.1.194 <(kubectl get pods -o wide)
app3 2/2 Running 2 80d 10.52.1.194 node-11 <none> <none>
bash -c grep -w 10.52.1.91 <(kubectl get pods -o wide)
app1 1/1 Running 0 10d 10.52.1.91 node-8 <none> <none>
bash -c grep -w 10.52.2.44 <(kubectl get pods -o wide)
app2 2/2 Running 0 80d 10.52.2.44 node-14 <none> <none>
bash -c grep -w 10.52.3.223 <(kubectl get pods -o wide)
app4 1/1 Running 0 6d10h 10.52.3.223 node-30 <none> <none>
最佳答案
你能试试这个吗:
grep -f <(kubectl exec -t redis -- redis-cli -a <redis-auth> client list | tail -n +2 | awk -F '[:= ]' '{ print $4 }' | sort | uniq) <(kubectl get pods -o wide)
如果您遇到问题,请先使用此命令进行故障排除:
cat <(kubectl exec -t redis -- redis-cli -a <redis-auth> client list | tail -n +2 | awk -F '[:= ]' '{ print $4 }' | sort | uniq)
cat <(kubectl get pods -o wide)
关于linux - 一个衬垫,用于使用 xargs 在其他命令生成的搜索文本上 grep 一个命令的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59514326/