期望脚本中的多个for循环

时间:2014-08-10 20:28:19

标签: shell loops unix expect

嗨,我在循环中不是那么好。有人可以帮助我进行sftp过程。该脚本应该读取2个文件,一个具有文件名,另一个具有localpath。脚本应该循环进入path1并从源到目标获取file1。但它只能获得一个文件。我的意思是脚本或循环只执行一次循环。你能帮助我吗?或者不是for循环,任何人都可以用其他方式写。在此先感谢.. !!

The bkupdirs file contains:
bkupdir1
bkupdir2
bkupdir3
dkupdir4
The backupfiles_list file contains:
bkupfile1
bkupfile2
bkupfile3
bkupfile4 

#!/usr/bin/expect -d
set sourceServer servername;
set f [open "/scripts/bkupdirs"]
set localbkuppaths [split [read $f] "\n"]
close $f
# Get the commands to run, one per line
set f [open "/dba/backupfiles_list"]
set srcbkupfiles [split [read $f] "\n"]
close $f

# Iterate over the hosts
foreach lbkuppath $localbkuppaths {
    spawn sftp id1@$sourceServer
    expect "password:"
    send "passwd\n";
    expect "sftp>"
    send "lcd $lbkuppath\n" 

# Iterate over the commands
foreach srcbkupfile $srcbkupfiles {
    expect "sftp>"
    send "get $srcbkupfile\n"
    expect "sftp>"
    send "bye\n"
}

# Tidy up
#    send "exit\n"
#    expect eof
#       interact
close
}

After the first iteration successfully it is not going to second value or 2nd iteration and have error msg like this:
send "bye\n"..."
("foreach" body line 10)
invoked from within
"foreach lbkuppath $localbkuppaths {
spawn sftp id1@$sourceServer
expect "password:"
send "passwd\n";
    expect "sftp>"
    send "lcd $l..."
(file "/db2dba/d1/master_scripts/sftpfull" line 13)

1 个答案:

答案 0 :(得分:0)

尝试将文件设置为不同的变量,如:

#!/usr/bin/expect -d
set f1 [open "/scripts/bkupdirs"]
set localbkuppaths [split [read $f1] "\n"]
close $f1
# Get the commands to run, one per line
set f2 [open "/dba/backupfiles_list"]
set srcbkupfiles [split [read $f2] "\n"]
close $f2