首页 > 解决方案 > 在期望脚本中获取多行输出

问题描述

我在文件中有以下代码,test.sh它输出目录中的文件列表:

log_user 0
spawn sftp -oPort=22 username@hostname
expect "password:"
send "password123\n"
set prompt "sftp>";
set command "ls -l upload/External/*";
expect "$prompt"    ;# wait for prompt
send   "$command\r" ;# send command
expect "$command\r" ;# discard command echo
expect -re "(.*)\r" ;# match and save the result
puts "$expect_out(0,string)"
send -- "exit\r"
expect eof

然而,在执行时/usr/bin/expect test.sh我只得到一行结果:

drwxrws---    0 300096661 300026669     4096 Apr 13 19:48 upload/External/file1.csv

我尝试将 puts 命令更改为,puts "$expect_out(buffer)"但它仍然是一样的。我期待该目录中存在的所有 5 个文件

标签: unixsftpexpect

解决方案


推荐阅读