首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Expect模块中字符串中的字符转义单引号

Expect模块中字符串中的字符转义单引号
EN

Stack Overflow用户
提问于 2018-05-28 19:27:00
回答 1查看 1.1K关注 0票数 0

我已经创建了一本.YML行动手册。

在里面放一些代码,效果很好。

然后,我遇到了一个问题--我正在尝试自动化某个应用程序的部署。应用程序提示用户进行交互。不幸的是,"yes | command“不起作用。它只是被忽略,并仍然提示。

因此,我决定使用Expect模块。

当前代码如下所示:

- hosts: all
  remote_user: someuser
  gather_facts: yes

  tasks:
    - name: "Copy files"
      copy: src=../somefiles/ dest=/tmp

    - name: "Execute some script"
      shell: sudo /tmp/script.sh

    - name: "Execute app"
      expect:
        command: /bin/bash -c 'sudo /tmp/app arguments'
        responses:
        "'Press 'y' to confirm ('s' to skip, 'a' to abort):'": "y"
      echo: y

我已经将预期的行括在双引号中。但是,由于预期的行有单引号('),它似乎打破了语法。

错误输出如下:

ERROR! Syntax Error while loading YAML.


The error appears to have been in 'deploy.yml': line 16, column 1, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

        responses:
        "'Press 'y' to confirm ('s' to skip, 'a' to abort):'": "y"
^ here
This one looks easy to fix.  It seems that there is a value started
with a quote, and the YAML parser is expecting to see the line ended
with the same kind of quote.  For instance:

    when: "ok" in result.stdout

Could be written as:

   when: '"ok" in result.stdout'

Or equivalently:

   when: "'ok' in result.stdout"
We could be wrong, but this one looks like it might be an issue with
unbalanced quotes.  If starting a value with a quote, make sure the
line ends with the same set of quotes.  For instance this arbitrary
example:

    foo: "bad" "wolf"

Could be written as:

    foo: '"bad" "wolf"'

我已经尝试过使用反斜杠()对单引号和双引号进行字符转义。它们都不起作用。根据我引用的顺序,我要么让这个看起来很容易修复。或者直接去找坏狼。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-28 19:51:40

问题不在于引号,而在于字符串中的特殊字符。Expect模块执行正则表达式匹配,因此响应字符串必须符合正则表达式

这意味着必须转义特殊字符,如圆括号和逗号

工作示例如下所示:

#!/bin/bash
echo "Press 'y' to confirm ('s' to skip, 'a' to abort):"
read response
echo $response

- connection: local
  hosts: localhost
  tasks:
    - name: "Execute app"
      expect:
        command: /tmp/run.sh
        responses:
          Press 'y' to confirm \('s' to skip\, 'a' to abort\):: "y"
        echo: yes
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50565339

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档