【问题标题】:Expect script execution status期望脚本执行状态
【发布时间】:2010-11-18 17:23:56
【问题描述】:

在下面的expect脚本中如何在expect脚本中获取rsync的退出状态 if [$? -eq 0] 抛出错误为

         invalid command name "$?"
         while executing
         "$? -eq 0 "
         invoked from within
        "if [ $? -eq 0 ]"
       (file "./tmp544.sh" line 7)


    #!/usr/bin/expect -f

    eval spawn ssh -l root 174.54.87.12 rsync /root/project /usr/project
    expect "root@174.54.87.12's password: $"
    send "\xxxxxxx\n"
    expect "\\$ $"
    if [ $? -eq 0 ]
    then
      echo "11"
    else
      echo "22"
    fi

【问题讨论】:

    标签: linux ssh expect


    【解决方案1】:
    if [$? -eq 0]
    

    是一个 shell 结构,你在一个期望脚本中。 我认为您应该将 rsync 部分与 ssh 部分分开,并使用远程 shell 来评估 rsync 状态:

    eval spawn ssh -l root 174.54.87.12
    expect "root@174.54.87.12's password: $"
    send "\xxxxxxx\n"
    expect "\\$ $"
    send "rsync /root/project /usr/project"
    expect "\\$ $"
    send "echo $?"
    # Examine the output of $?
    

    $?可在交互式 shell 中使用,不仅在脚本中,您还可以在命令行中使用它,而且看起来是这样做的好时机!

    我对expect不熟悉,但我猜想expect "\$ $" 是在等待shell提示符,所以我一味的复制粘贴。

    【讨论】:

    • expect {\$ $} 可以说是比期望 "\\$ $" 更惯用的替代方案
    猜你喜欢
    • 2015-05-10
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多