A
A
Alexander Nazarov2022-04-08 19:14:55
bash
Alexander Nazarov, 2022-04-08 19:14:55

How to loop the result of a bash command?

There is a command that gives a list of Docker container names.
What is the correct way to write a loop in Bash?
How to substitute these names in a loop by executing another command?
Well, or maybe your options, the best ways to get the result of the team.

container=$(docker container ls --format 'table {{.Names}}');

  while $container
  do
    docker exec -u root -it $container netstat -natu | grep -c ESTABLISHED;
  done

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mureevms, 2022-04-08
@wergio

To pass command output arguments as a single value, you need to have the correct output. Those. instead of

container=$(docker container ls --format 'table {{.Names}}')
gotta do
container=$(docker container ls --format '{{.Names}}')

In the end it should look something like this:
#!/bin/bash
container=$(docker container ls --format '{{.Names}}');

 for i in $container
 do
    docker exec -u root -it $i netstat -natu | grep -c ESTABLISHED;
 done

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question