简体   繁体   English

在bash中解析Dockerfile的卷数组

[英]Parsing volume array of Dockerfile in bash

I'm working on a management script for Docker containers. 我正在为Docker容器管理脚本。 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. 通常,这些变量已在Dockerfile中定义,因此默认值应为读取这些值。

I'm having some trouble with the array format used in these Dockerfiles. 我在这些Dockerfile中使用的数组格式遇到了一些麻烦。 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. 如果我有卷定义: VOLUME ["/root/", "/var/log/"]该文件脚本应该能够弄清楚/ root /和/ var / log,但我无法做到这一点然而。

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 '(?<=").+?(?=")' 到目前为止,我已经可以使用grep VOLUME Dockerfile | cut -c 8- | grep -o -P '(?<=").+?(?=")'从文件中获取“ / root /”,“,”和“ / var / log” grep VOLUME Dockerfile | cut -c 8- | grep -o -P '(?<=").+?(?=")' grep VOLUME Dockerfile | cut -c 8- | grep -o -P '(?<=").+?(?=")' grep VOLUME Dockerfile | cut -c 8- | grep -o -P '(?<=").+?(?=")' but this stil includes the ", " which should be left out. grep VOLUME Dockerfile | cut -c 8- | grep -o -P '(?<=").+?(?=")'但此stil包括应省略的“,”。

Does anyone have suggestions about how to parse this properly? 有人对如何正确解析有建议吗?

awk to the rescue! awk解救!

$ 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. 通过设置定界符,您可以提取所有字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM