linux - 不使用通配符的预期工具不匹配换行符

标签 linux bash shell centos expect

我正在编写一个脚本来使用 Expect 远程安装 linux 软件更新(使用 .bin 更新工具)。我在匹配一个 block 时遇到了麻烦,我尝试了一堆变体,但都没有成功。我已经让其他类似的脚本工作了,但是我使用通配符来匹配换行符,在这种情况下,我需要确保文件列表正是我所期望的,所以我不想打破使用任何可能隐藏不匹配的通配符。
有人可以看看并告诉我我做错了什么吗?
控制台输出,普通

Checking for local modifications.

List of modifications made within Jira directories.

The following provides a list of file modifications within the
atlassian-jira directory.

Modified files:
    atlassian-jira/WEB-INF/classes/seraph-config.xml
    bin/setenv.sh
    conf/server.xml
Removed files:
    (none)
Added files:
    atlassian-jira/WEB-INF/classes/crowd.properties

[Enter]
我最近尝试匹配它并发送 Enter(换行符仅添加到 Stack Overflow,脚本中不存在

expect {*Checking for local modifications*Modified files:\r\n\tatlassian-jira/WEB-INF/classes/seraph-config.xml\r\n\tbin/setenv.sh\r\n\tconf/server.xml\r\nRemoved files:\r\n\t(none)\r\nAdded files:\r\n\tatlassian-jira/WEB-INF/classes/crowd.properties\r\n\r\n\[Enter\]}


当我跑过 expect-d标志,这是它打印的内容:

expect: does "\r\nn\r\n\r\nChecking for local modifications.\r\n\r\nList of modifications made within Jira directories.\r\n\r\nThe following provides a list of file modifications within the\r\natlassian-jira directory.\r\n\r\nModified files:\r\n\tatlassian-jira/WEB-INF/classes/seraph-config.xml\r\n\tbin/setenv.sh\r\n\tconf/server.xml\r\nRemoved files:\r\n\t(none)\r\nAdded files:\r\n\tatlassian-jira/WEB-INF/classes/crowd.properties\r\n\r\n[Enter]\r\n" (spawn_id exp5) match glob pattern "*Checking for local modifications*Modified files:\r\n\tatlassian-jira/WEB-INF/classes/seraph-config.xml\r\n\tbin/setenv.sh\r\n\tconf/server.xml\r\nRemoved files:\r\n\t(none)\r\nAdded files:\r\n\tatlassian-jira/WEB-INF/classes/crowd.properties\r\n\r\n\[Enter\]"? no


我正在从外部 bash 脚本运行,如下所示:
#!/usr/bin/env bash
set -e

sudo expect -d << EOD
    spawn ${INSTALLER_PATH}

    expect_before timeout { exit 1 }

    expect {Match phrase} {
        send -- "\r"
    }

    ...

    expect eof

EOD
** 2021 年 4 月 5 日更新 **
我尝试了@pynexj 的建议使用 ""而不是 {}包围我的表情(下),它仍然不起作用。
新脚本
sudo expect -d << EOD
    ...
    expect "\r\nn\r\n\r\nChecking for local modifications.\r\n\r\nList of modifications made within Jira directories.\r\n\r\nThe following provides a list of file modifications within the\r\natlassian-jira directory.\r\n\r\nModified files:\r\n\tatlassian-jira/WEB-INF/classes/seraph-config.xml\r\n\tbin/setenv.sh\r\n\tconf/server.xml\r\nRemoved files:\r\n\t(none)\r\nAdded files:\r\n\tatlassian-jira/WEB-INF/classes/crowd.properties\r\n\r\n\[Enter\]\r\n" {
        send -- "\r"
    }

    expect eof

EOD
我运行了通过 Beyond Compare 收到的调试输出,打印出来的两个字符串 100% 匹配。
更新了调试输出

expect: does "\r\nn\r\n\r\nChecking for local modifications.\r\n\r\nList of modifications made within Jira directories.\r\n\r\nThe following provides a list of file modifications within the\r\natlassian-jira directory.\r\n\r\nModified files:\r\n\tatlassian-jira/WEB-INF/classes/seraph-config.xml\r\n\tbin/setenv.sh\r\n\tconf/server.xml\r\nRemoved files:\r\n\t(none)\r\nAdded files:\r\n\tatlassian-jira/WEB-INF/classes/crowd.properties\r\n\r\n[Enter]\r\n" (spawn_id exp5) match glob pattern "\r\nn\r\n\r\nChecking for local modifications.\r\n\r\nList of modifications made within Jira directories.\r\n\r\nThe following provides a list of file modifications within the\r\natlassian-jira directory.\r\n\r\nModified files:\r\n\tatlassian-jira/WEB-INF/classes/seraph-config.xml\r\n\tbin/setenv.sh\r\n\tconf/server.xml\r\nRemoved files:\r\n\t(none)\r\nAdded files:\r\n\tatlassian-jira/WEB-INF/classes/crowd.properties\r\n\r\n[Enter]\r\n"? no


两个字符串重叠
您可以在 Stack Overflow 中横向滚动这些内容,以查看它们是否 100% 匹配:
# command output on top
\r\nn\r\n\r\nChecking for local modifications.\r\n\r\nList of modifications made within Jira directories.\r\n\r\nThe following provides a list of file modifications within the\r\natlassian-jira directory.\r\n\r\nModified files:\r\n\tatlassian-jira/WEB-INF/classes/seraph-config.xml\r\n\tbin/setenv.sh\r\n\tconf/server.xml\r\nRemoved files:\r\n\t(none)\r\nAdded files:\r\n\tatlassian-jira/WEB-INF/classes/crowd.properties\r\n\r\n[Enter]\r\n
\r\nn\r\n\r\nChecking for local modifications.\r\n\r\nList of modifications made within Jira directories.\r\n\r\nThe following provides a list of file modifications within the\r\natlassian-jira directory.\r\n\r\nModified files:\r\n\tatlassian-jira/WEB-INF/classes/seraph-config.xml\r\n\tbin/setenv.sh\r\n\tconf/server.xml\r\nRemoved files:\r\n\t(none)\r\nAdded files:\r\n\tatlassian-jira/WEB-INF/classes/crowd.properties\r\n\r\n[Enter]\r\n
# Expect expression underneath

最佳答案

在 Tcl 中,嵌入大括号 ( \r\n ) 中的反斜杠转义字符串 (如 {} ) 没有特殊含义。你应该在这里使用双引号。

$ tclsh
% puts {foo\tbar\r\n}
foo\tbar\r\n
% puts "foo\tbar\r\n"
foo     bar

%

根据 Tcl doc :
  • [6] Braces.

    If the first character of a word is an open brace ({) and rule [5] does not apply, then the word is terminated by the matching close brace (}). Braces nest within the word: for each additional open brace there must be an additional close brace (however, if an open brace or close brace within the word is quoted with a backslash then it is not counted in locating the matching close brace). No substitutions are performed on the characters between the braces except for backslash-newline substitutions described below, nor do semi-colons, newlines, close brackets, or white space receive any special interpretation. The word will consist of exactly the characters between the outer braces, not including the braces themselves.


但请注意,当您使用双引号时。匹配 [Enter]你需要写\\\[Enetr\\\]因为默认情况下 expect语句使用 glob 样式匹配。例如:
$ expect -c 'spawn -noecho bash -c {echo "[Enter]"}; expect eof'
[Enter]
$ expect -c 'spawn -noecho bash -c {echo "[Enter]"}; expect "\\\[Enter\\\]"; expect eof'
[Enter]
$ expect -c 'spawn -noecho bash -c {echo "[Enter]"}; expect -ex "\[Enter\]"; expect eof'
[Enter]
$

更新:
刚刚注意到您正在使用 Bash 的此处文档。然后你必须再做一级报价。
$ expect << EOF
> spawn -noecho bash -c {echo "[Enter]"}
> expect "\\\\\\[Enter\\\\\\]"
> expect eof
> EOF
[Enter]
$
或者您可以使用 << "QUOTED_STRING_HERE" :
$ expect << 'EOF'
> spawn -noecho bash -c {echo "[Enter]"}
> expect "\\\[Enter\\\]"
> expect eof
> EOF
[Enter]
$

根据 man bash , 对于 << word :

If any part of word is quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded.

关于linux - 不使用通配符的预期工具不匹配换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66914319/

相关文章:

c - 如何删除从父进程继承的所有环境变量?

linux - 如何通过 ssh pgrep,或将 pgrep 用作更大的 bash 命令?

linux - 需要有关 shell 脚本的帮助以获得预期输出

bash - shell 脚本中的 SPRINTF?

bash - 递归运行 `tmutil isexcluded`

linux - make 命令和运行 build .sh 脚本之间的主要区别是什么?

python - Ipython bash/shell 单元魔法 : can I have persistent variables between cells?

c# - 是否建议在不使用反向代理的情况下在 Linux 上运行 Kestrel?

python - 为什么我的 python 脚本在我退出 putty 后仍在 EC2 的后台运行?

linux - 在转储到文件之前处理从 bash 脚本中的 find 命令获取的字符串