0

I'm working on a management script for Docker containers. Right now the user has to configure certain variables before using it. Often, these variables are already defined in the Dockerfile so the default should be to read those values.

I'm having some trouble with the array format used in these Dockerfiles. If I have the volume definition: VOLUME ["/root/", "/var/log/"] the file script should be able to figure out /root/ and /var/log, I haven't been able to accomplish this yet.

So far I have been able to get "/root/" ", " and "/var/log" out of the file using grep VOLUME Dockerfile | cut -c 8- | grep -o -P '(?<=").+?(?=")' but this stil includes the ", " which should be left out.

Does anyone have suggestions about how to parse this properly?

Sven van de Scheur
  • 1,809
  • 2
  • 15
  • 31

1 Answers1

1

awk to the rescue!

$ echo VOLUME ["/root/", "/var/log/"] | 
  awk -F'[ ,\\[\\]]' '/VOLUME/{for(i=3;i<=NF;i+=2) print $i}'

/root/
/var/log/

by setting the delimiters you can extract all the fields.

karakfa
  • 66,216
  • 7
  • 41
  • 56