这就是我的 docker-compose.yml 的样子。
nginx:
container_name: 'nginx'
image: 'nginx:1.11'
restart: 'always'
ports:
- '80:80'
- '443:443'
volumes:
- '/opt/nginx/conf.d:/etc/nginx/conf.d:ro'
links:
- 'anything'
Run Code Online (Sandbox Code Playgroud)
现在我需要通过 shell 脚本(在 ubuntu 服务器上)添加一些内容。我不太确定是否有可能:
nginx/links
,如果它不存在newthing
如果不存在 newthing-block 则追加块新内容应如下所示:
nginx:
container_name: 'nginx'
image: 'nginx:1.11'
restart: 'always'
ports:
- '80:80'
- '443:443'
volumes:
- '/opt/nginx/conf.d:/etc/nginx/conf.d:ro'
- '/etc/letsencrypt:/etc/letsencrypt'
links:
- 'anything'
- 'newthing'
newthing:
container_name: foo
image: 'newthing:1.2.3'
restart: always
hostname: 'example.com'
Run Code Online (Sandbox Code Playgroud) 我有一个 yml 文件
spring:
datasource:
url: url
username:test
password: testpwd
api:
security:
username:foo
password: foopwd
Run Code Online (Sandbox Code Playgroud)
我只想在 Linux 机器上使用命令行更新第一次出现或用户名和密码,使其看起来像这样:
spring:
datasource:
url: url
username:toto
password: totopsw
api:
security:
username:foo
password: foopwd
Run Code Online (Sandbox Code Playgroud)
当我尝试:
sed -i -e 's!^\(\s*username:\)[^"]*!\1toto!' test.yml
Run Code Online (Sandbox Code Playgroud)
他更改了所有用户名
我有这个 awk 语句,它读取一个 YAML 文件并输出一个特定的值。我需要在一个循环中循环这个 awk,我从一个值列表中读取一个键值并将该键传递给 awk。
YAML 文件具有以下结构:
abc:
NAME: Bob
OCCUPATION: Technician
def:
NAME: Jane
OCCUPATION: Engineer
Run Code Online (Sandbox Code Playgroud)
说我想关键abc
OCCUPATION
的价值TECHNICIAN
,通过谷歌搜索我设法建立一个awk语句,让我想要什么
> awk 'BEGIN{OFS=""} /^[^ ]/{ f=/^abc:/; next } f{ if (sub(/:$/,"")) abc=$2; else print abc,$1 $2}' test.yml| grep "OCCUPATION:" | cut -d':' -f2
Technician
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用这个循环,将 -v 选项传递给 awk 似乎没有任何意义:
items="abc,def"
for item in $(echo $items | sed "s/,/ /g");
do
echo $item;
awk -v name="$item" 'BEGIN{OFS=""} /^[^ ]/{ f=/^\name:/; next } f{ if (sub(/:$/,"")) name=$2; …
Run Code Online (Sandbox Code Playgroud) 我正在使用 bash shell。我有一个 YAML 文件,我想从中删除某些文本块。
/image-content:
post:
operationId: createEventPublic
summary: Process events
description: Process events
parameters: []
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Content'
responses:
'201':
description: Created
content:
application/json:
schema:
$ref: '#/components/schemas/Content'
/text-content:
post:
operationId: createStaticText
summary: Process text events
description: Process text events
parameters: []
requestBody:
...
Run Code Online (Sandbox Code Playgroud)
我想删除(例如)路径包含“图像内容”的文本块。通常我可以用它来删除带有该文本的一行
sed -i '/image-content/d' ./infile
Run Code Online (Sandbox Code Playgroud)
但我不太清楚如何替换之后的每一行,直到下一行以两个空格和一个“/”(例如“/”)开头。在上面,我想删除所有内容,直到
/text-content:
Run Code Online (Sandbox Code Playgroud)
编辑:虽然这可能不是有效的 openapi 3 swagger,但我相信它仍然是一个有效的 YAML 文件
openapi: 3.0.0
components:
/static/image-content:
post:
type: hello
/api/hello:
post:
type: hello
/static/css-content:
post:
type: hello
Run Code Online (Sandbox Code Playgroud)
最后,我想删除以“/static”开头的块。所以结束文档将是 …
我有许多 Markdown 文件,顶部有一个 YAML 元数据块:
---
title: title of work
author: author name
author-sort: name, author
published: N
date: XXXX-XX-XX
pub-number: XXXXX
embedded-title: false
---
**title**
a piece of indertimate
length that could be a few lines of many hundreds
---[author name](author link)
---found in [source](source link)
Run Code Online (Sandbox Code Playgroud)
我正在尝试找到一种提取元数据块的方法(这样我可以将其输入到yamllint
,但后来也可以输入其他内容)。awk 似乎是正确的工具,但是当我不理解 awk 时,我想出的最好的方法是:
awk '/^---$/ {printline = 1; print; next} /^---$/ {printline = 0} printline'
Run Code Online (Sandbox Code Playgroud)
这只是显示了整个文件,我尝试限制---
它们自己的行是行不通的(也许这里没有其他的)!
我有这个文件,如果没有注释该行,我想选择该masters/hosts
部分下的所有 IP 地址。我尝试过这个,但没有成功。sed 's/.*\ \([0-9\.]\+\).*/\1/g'
metal:
children:
masters:
hosts:
dev0: {ansible_host: 172.168.1.10}
# dev1: {ansible_host: 185.168.1.11}
# dev2: {ansible_host: 142.168.1.12}
workers:
hosts: {}
# dev3: {ansible_host: 172.168.1.13}
Run Code Online (Sandbox Code Playgroud) 我有一个文件,其中的条目key: value
格式如下:
猫数据.txt
name: 'tom'
tom_age: '31'
status_tom_mar: 'yes'
school: 'anne'
fd_year_anne: '1987'
name: 'hmz'
hmz_age: '21'
status_hmz_mar: 'no'
school: 'svp'
fd_year_svp: '1982'
name: 'toli'
toli_age: '41'
Run Code Online (Sandbox Code Playgroud)
同样...
我只需要查找并打印那些key: value
具有重复键作为单个条目的键。
下面的代码让我得到重复的键
cat data.txt | awk '{ print $1 }' | sort | uniq -d
name:
school:
Run Code Online (Sandbox Code Playgroud)
但是,我想要输出将重复键的值连接在一行中。
预期输出:
name: ['tom', 'hmz', 'toli']
school: ['anne', 'svp']
tom_age: '31'
status_tom_mar: 'yes'
fd_year_anne: '1987'
hmz_age: '21'
status_hmz_mar: 'no'
fd_year_svp: '1982'
toli_age: '41'
Run Code Online (Sandbox Code Playgroud)
你能建议一下吗?
我们想要实现一个工作流,其中包括拥有 YAML 配置文件,我们希望在实际使用它们之前更改其中的一些内容,我们正在寻找一个好的命令行工具来执行此操作。
这可以像在现有节点中设置特定于目标的值(如 URL)一样简单,但最好能够添加或修改 YAML 文件的整个块。在一个完美的世界中,我想要像 XSLT 中的轴一样强大的东西。
我们将在 Docker 文件中与 Maven 一起使用它,我们偏爱基于 Debian 的镜像,但不是严格要求。
我在 YAML 文件中有多个配置,我需要使用 Bash 脚本更改一些参数。是否可以?我想避免使用任何外部依赖。
我的 YAML 看起来像
%YAML 1.2
---
name: mic
components:
- name: Mic
parameters:
period_count: 4
alsa_device_name: "pulse"
---
name: speaker
components:
- name: Speaker
parameters:
period_duration_ms: 20
period_count: 4
alsa_device_name: "pulse"
Run Code Online (Sandbox Code Playgroud)
我希望它表现得像如果我提供--mic logitec --speaker hk34
那么就应该修改alsa_device_name
两个mic
和speaker
我的config来
%YAML 1.2
---
name: mic
components:
- name: Mic
parameters:
period_count: 4
alsa_device_name: "logitec"
---
name: speaker
components:
- name: Speaker
parameters:
period_duration_ms: 20
period_count: 4
alsa_device_name: "hk34"
Run Code Online (Sandbox Code Playgroud)
是否可以仅使用 Bash 来实现,如果可以,如何实现?现在我正在使用 Python …
给定一个 YAML 文件example.yaml
:
node:
sub_node:
get_this:
Run Code Online (Sandbox Code Playgroud)
我想获得一个包含get_this
使用 Mike Farahyq
和字符串的变量sub_node
yaml="$(cat example.yaml)"
nodename=sub_node
sub_yaml= "$(echo "$yaml" | yq -r '.$nodename' )";
# also tried -> sub_yaml= "$(echo "$yaml" | yq -r '.'"$nodename" )";
Run Code Online (Sandbox Code Playgroud)
请注意,这是一个人为的示例,实际上,该sub_node
字符串事先并不知道,因此我需要替换该$nodename
字符串。
我似乎无法弄清楚如何转义 所需的单引号查询字符串yq
。
我该怎么做?
我正在尝试替换 YAML 文件的以下部分:
ssl:
enabled: false
Run Code Online (Sandbox Code Playgroud)
阅读
ssl:
enabled: true
Run Code Online (Sandbox Code Playgroud)
我试过这个,失败了: sed -i s/ssl:\n enabled: false/ssl:\n enabled: true/g
我正在用letsencrypt和nginx做一些docker。在我遵循的教程中,有一个用于 certbot 容器的命令,它允许每 12 小时检查一次证书的有效性。
/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'
Run Code Online (Sandbox Code Playgroud)
我做 bash 已经有一段时间了,但wait $${!}
直到今天我还从未见过这样的事情?是什么$${!}
意思?
编辑: 该命令是 docker-compose.yaml 的一部分,所以完整的命令是:
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"