[英]Expect command not always printing output to file
我有一个main_parent_script.sh
,我在其中又调用了一个具有以下命令的parent_script.sh
:
/usr/bin/expect sftp_pass.sh list 22 sample_user hostname.hc.uk pass@123 get/FOR_20210420125933_NOTIFICATION_HEADER.json.zip > /temp/src_list.txt
每当我运行主父脚本时,它都不会填充src_list.txt
文件。 但是当我单独运行 sftp_pass.sh 命令时,它会使用以下 o/p 正确填充src_list.txt
文件:
-rwxrwxrwx 0 300096102 300096102 576 May 24 15:04 get/FOR_20210420125933_NOTIFICATION_HEADER.json.zip
sftp>
此外,我观察到 output 在由主父脚本运行时正确填充,仅当 ls 命令的 output 提供 2 个或更多文件时。
下面是 sftp_pass.sh 脚本的内容:
log_user 0
##-------------Start of script----------------
set expectedcmd [lindex $argv 0]
set port [lindex $argv 1]
set user [lindex $argv 2]
set host [lindex $argv 3]
set pass [lindex $argv 4]
if {$expectedcmd=="list"} {
set file [lindex $argv 5]
spawn sftp -oPort=$port $user@$host
expect "password:"
send "$pass\n"
set prompt "sftp>";
set command "ls -l $file";
}
##---------------------------------
expect "$prompt" ;# wait for prompt
send "$command\r" ;# send command
expect "$command\r" ;# discard command echo
log_user 1
sleep 5
expect -re "(.*)\r" ;# match and save the result
expect eof
send -- "exit\r"
##-------------End of script--------------------
我什至尝试使用一个log_file
来 output 在脚本里面执行。 但我仍然有同样的问题。
试试这样:
...
expect "$command\r" ;# discard command echo
expect *
send_user -- $expect_out(buffer)
log_user 1
sleep 5
expect -re "(.*)\r" ;# match and save the result
expect eof
一些代码审查评论:
expect eof
应该在send exit
之后 - 当前位置没有错误,除了您的脚本将“挂起” $timeout 秒,等待 sftp 连接关闭,看不到它,然后发送退出命令。
如果第一个参数不是“列表”会发生什么? 您不会spawn
任何进程,但随后会出现提示。
如果预期模式是正确的,您不应该需要sleep 5
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.