本文介绍了如何在带有 YAML 的“脚本:"中使用多行命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Travis CI 的存储库,在 .travis.yml 中,我有这一行:

I have a repository that uses Travis CI, and in the .travis.yml there I have this line:

script:
- vim -Nu <(cat <<-EOF
  set nocompatible |
  filetype off
  EOF
  ) -c 'Script' > /dev/null

遗憾的是这不起作用,因为它被转换成一行并像这样执行:

Sadly this doesn't work, as this is transformed into a single line and is executed like this:

vim -Nu <(cat <<-EOF set no compatible | filetype off | EOF ) -c 'Script' > /dev/null

这使得 EOF 标签不起作用,因为 EOF 需要在一行中.另一种方法是使用这样的普通引号:

This makes the EOF tag not working, as EOF needs to be in a single line.An alternative would be to just use normal quotes like this:

script:
- vim -Nu <(cat 'set nocompatible |
  filetype off
  ) -c 'Script' > /dev/null

哪个有效,并且很好,但我觉得必须有一种方法可以将换行符插入到 .travis.yml 中.我现在有一个选择,但我可能不会在未来.那你是怎么做的呢?

Which works, and is fine, but I feel there must be a way to insert newlines into a .travis.yml. I have an alternative now, but I may not in the future. So how do you do it?

推荐答案

在 YAML 中,您可以通过使用 "" 引用和转义换行符来指定标量中的换行符 (\n),或者,这对您的情况更自然,使用 literal样式块标量:

In YAML you can specify newlines in a scalar by using "" quoting and escaping the newlines (\n), or, and that is more natural for your case, by using a literal style block scalar:

script:
- |
  vim -Nu <(cat <<-EOF
  set nocompatible |
  filetype off
  EOF
  ) -c 'Script' > /dev/null

这是一个标量,以带有 |(管道符号)的行开头,后跟保留换行符的多行.

This is a scalar starting with a line with a | (pipe symbol), followed by multiple lines for which the line-breaks are preserved.

  • 这些行通常是缩进的(例外:单个顶级文字样式块标量).
  • |之后可以有修饰符:1-9,当你的第一行以空格开头时使用;+, - 影响最终换行符的剥离(通常折叠为一个).
  • The lines are normally indented (exception: a single top-level literal style block scalar).
  • After the | there can be modifiers: 1-9, used when your first line starts with spaces; +, - to influence stripping of final newlines (normally collapsed into one).

这篇关于如何在带有 YAML 的“脚本:"中使用多行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 16:37