Load Stitch
Load Stitch

Reputation: 167

Copy output of a shell command to a variable and need to make output data's comma separated

I am trying to get the IP's of the docker containers and need to pass the IP's as comma separated.

Using below command i am able to get the container ip's

sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}' $(sudo docker ps -q --filter "name=slave")

output-->

> 10.17.0.4
> 10.17.0.3
> 10.17.0.2

Need help to save the output IP's to a variable and process IP's to be comma separated like 10.17.0.4,10.17.0.3,10.17.0.2

Upvotes: 0

Views: 200

Answers (1)

Adiii
Adiii

Reputation: 60074

You are using an old version of Docker (info). So the below command will not work in the new version of Docker

sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}' $(sudo docker ps -q --filter "name=slave") | sed -n -e 'H;${x;s/\n/,/g;s/^,//;p;}'

In the New version of Docker, you can try the below command.

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -q  --filter ancestor=adminer) | sed -n -e 'H;${x;s/\n/,/g;s/^,//;p;}'

You can filter base on the image using below command in New version, this will return all the container ID, other wise old commands will return only one container in the new version.

docker ps -q  --filter ancestor=image_name

enter image description here

Upvotes: 1

Related Questions