0

I am running a script inside a server to get a list of services and fetch corresponding logs of each service, below is my command

ssh root@xxx.xxx.xxx.xxx << 'EOF'
service_list=$(docker ps -a | grep services | awk '{print $1}')
echo $service_list
EOF

output is

    service1 service2 service3

I want to cut this output or store them in an array so i can execute

docker logs $service_list[0] and so on

I tried to store the output to an array

ssh root@xxx.xxx.xxx.xxx << 'EOF'
service_list=$(docker ps -a | grep services | awk '{print $1}')
echo $service_list
echo "print first value in array"
echo $service_list[0]
echo "print second value in array"
echo $service_list[1]
echo "print third value in array"
echo $service_list[2]
EOF

my output

service1 service2 service3
print first value in array
service1 service2 service3
print second value in array

print third value in array

a blank line is printed for second and third value in the array,for first value of array the complete service list is printed.

I am expecting an output like this while printing values in array

service1
service2
service3

any leads would be helpful

Butner
  • 81
  • 1
  • 9
  • `readarray -t service_list < <(docker ps -a | awk '/service/ { print $1}')` – Charles Duffy Jul 30 '19 at 00:12
  • And then `echo "${service_list[0]}"` -- the curly brackets are mandatory, or else you only print the first item with `[0]` at the end of a string. Whereas to print a whole array, use `declare -p service_list`. – Charles Duffy Jul 30 '19 at 00:12

0 Answers0