【问题标题】:expect is not working for non-English language期望不适用于非英语语言
【发布时间】:2021-04-03 19:53:54
【问题描述】:

我正在使用 expect 和 shell 代码在 CentOS 7.9 上进行 Postgres 数据库自动备份。我的代码在英语环境中运行良好,但在日语环境中失败。这是我的期望代码

#!/usr/bin/expect
set homeDirectory "/home/kds/DB_BACKUP"
set dbPassword "manager"

log_file $homeDirectory/LOG/db_backup.log;

#Execute database backup script

spawn $homeDirectory/./backupexecutor.sh

set found 0
while {$found < 1} {
expect {
"パスワード: $"    {send "$dbPassword\r"}
"^Rollout Done "   {set found 1}
"^Rollout Updated " {set found 1}
}
}

expect eof

检查日志文件后,很明显,expect 无法为パスワード: $ 关键字发送密码。我也试过*スワー*,但结果相同。如果将我的操作系统语言从日语更改为英语并将パスワード 替换为Password,则代码可以正常工作。有什么方法可以使用不同的语言来工作吗?

【问题讨论】:

  • 我没有任何经验可以提供帮助,但expect 使用tcl,因此您可能会从其i18n 文档页面获得一些信息。确保将您在环境中设置为LANG= 等的内容添加到您的帖子中,以及您的文件采用什么编码。
  • 那个$ 字符,它是作为您希望看到的文字字符,还是正则表达式字符串结尾锚点?如果是后者,则您缺少 -re 标志。
  • 针对expect子句的$的推理做出回应
  • @glennjackman ,默认expect "something"表示expect -gl "something",$也有特殊含义。
  • 不正确。 glob特殊字符枚举here——$不在其中。 $ 仅在 regular expressions 中有特殊含义

标签: shell centos expect


【解决方案1】:

做了一个简单的测试,它对我有用(用Expect 5.45、Tcl 8.5.9、macOS 10.15 测试)。

[STEP 101] $ cat foo.sh
read -s -p 'Enter your パスワード: ' passwd
echo
echo "Your password is: $passwd"
[STEP 102] $ bash foo.sh
Enter your パスワード: foobar  <-- manual input
Your password is: foobar
[STEP 103] $
[STEP 104] $ cat foo.exp
spawn bash foo.sh
expect {
    "パスワード: $" {
        send "foobar\r"
    }
}
expect eof
[STEP 105] $ expect foo.exp
spawn bash foo.sh
Enter your パスワード:
Your password is: foobar
[STEP 106] $

我的地区信息:

[STEP 107] $ locale
LANG=""
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL="en_US.UTF-8"
[STEP 108] $

expect -d:

[STEP 109] $ expect -d foo.exp
expect version 5.45
argv[0] = expect  argv[1] = -d  argv[2] = foo.exp
set argc 0
set argv0 "foo.exp"
set argv ""
executing commands from command file foo.exp
spawn bash foo.sh
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {84009}

expect: does "" (spawn_id exp6) match glob pattern "\u30d1\u30b9\u30ef\u30fc\u30c9: $"? no
Enter your パスワード:
expect: does "Enter your \u30d1\u30b9\u30ef\u30fc\u30c9: " (spawn_id exp6) match glob pattern "\u30d1\u30b9\u30ef\u30fc\u30c9: $"? yes
expect: set expect_out(0,string) "\u30d1\u30b9\u30ef\u30fc\u30c9: "
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "Enter your \u30d1\u30b9\u30ef\u30fc\u30c9: "
send: sending "foobar\r" to { exp6 }

Your password is: foobar
expect: read eof
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "\r\nYour password is: foobar\r\n"
[STEP 110] $

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    相关资源
    最近更新 更多