预期产生:没有这样的文件或目录错误

时间:2019-07-25 17:56:05

标签: bash expect

我正在编写一个Expect脚本,该脚本将远程访问服务器“ server2”并执行fileexists.sh,它将查找文件是否存在于特定位置。

Bash脚本-fileexists.sh如下:

#!/bin/bash

if [ -s $1 ]; then 
   echo "non empty file exists."
else 
   echo "File doesnot exist or is empty."

期望脚本-expec如下:

#!/usr/bin/expect
set username [lindex $argv 0]
set password [lindex $argv 1]
set hostname [lindex $argv 2]
set rfile [lindex $argv 3]
set prompt "$username* ~]$ "

spawn ssh -q -o StrictHostKeyChecking=no $username@$hostname

expect {
    timeout {
        puts "\n\nConnection timed out"
        exit 1
    }

    "*assword:" {
        send "$password\r"
    }
}

expect {
    "$prompt" {
        puts "Logged in successfully.
    }
}

spawn ./fileexists.sh $rfile

set lineterminationChar "\r"
expect {
       $lineterminationChar   { append output $expect_out(buffer);exp_continue}
       eof                    { append output $expect_out(buffer)}
       }
puts $output

当我使用./expec用户通过server2 /apps/bin/file.p调用Expect脚本时,输出为“文件不存在或为空”。尽管该文件位于server2上的位置。

我使用了期望进行了检查: 如果{[文件存在$ rfile]} {放置“文件存在”}

我得到的输出是“文件存在”。

似乎是我无法弄清的东西。

1 个答案:

答案 0 :(得分:0)

要检查远程主机,请执行以下操作(我假设您正在登录到Linux计算机或具有GNU stat的计算机):未经测试

send -- "stat -c '%s' '$rfile'\r"

expect {
    -re {\r\n(\d+)\r\n} {
        puts "$rfile exists with size $expect_out(1,string)"
    }
    -gl {*No such file or directory*} {
         puts "$rfile does not exist"
    }
}

send "exit\r"
expect eof
相关问题