bash 如何在带有 YAML 的“脚本:”中使用多行命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38745696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:58:15  来源:igfitidea点击:

How to use multiline command in 'script:' with YAML?

bashcontinuous-integrationyamltravis-ci

提问by hgiesel

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

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

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

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

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

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

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?

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

回答by Anthon

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:

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

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.

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

  • 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).
  • 这些行通常是缩进的(例外:单个顶级文字样式块标量)。
  • 后面|可以有修饰符:1- 9,当你的第一行以空格开头时使用;+,-影响最终换行符的剥离(通常折叠为一个)。

回答by serghei

I use the folded block styleto achieve exactly the desired effect:

我使用折叠块样式来达到完全想要的效果:

script:
    - >
      valgrind
      --read-var-info=yes
      --error-exitcode=1
      --fullpath-after=
      --track-origins=yes
      --leak-check=full
      --num-callers=20
      --suppressions=$(pwd)/tests/zephir_parser.3.7.0.sup
      $(phpenv which php)
          -d variables_order=EGPCS
          run-tests.php
              -p $(which php)
              -d extension=$(pwd)/modules/zephir_parser.so
              -d variables_order=EGPCS
              -g "FAIL,XFAIL,BORK,WARN,LEAK,SKIP"
              --offline
              --show-diff
              --set-timeout 120