在shell脚本中循环运行expect命令

时间:2014-09-10 06:48:45

标签: bash shell scripting expect scp

我在文本文件中有文件名列表,需要使用scp命令将每个文件传输到服务器。我正在读取 Read.sh 的文件名,并将每个文件名传递给传输。 sh 脚本但scp没有在此传输脚本中执行命令。如果我单独运行 transfer.sh 并传递args,则其工作正常。

LIST.TXT

/home/kittu/file1.txt
/home/kittu/file2.txt
/home/kittu/file3.txt

Read.sh

#!/bin/bash
while read p; do
  echo $p
    ./transfer.sh "$p"
done <List.txt

transfer.sh

#!/usr/bin/expect -f

# get filename from command-line
set f [lindex $argv 0]

spawn scp "$f" user@192.168.4.151:/home/user/Desktop/ 
expect "password"
send "123\r"
interact

我只是将Read.sh作为

运行
>./Read.sh

输出:

/home/user/Desktop/file1.txt
spawn scp /home/mbox140/Desktop/test.sh mbox140@192.168.4.151:/home/mbox140/Desktop/videos/
user@192.168.4.151's password:

它没有执行下一个声明。请建议我任何解决方案。

1 个答案:

答案 0 :(得分:5)

尝试以下脚本,更改是Transfer.sh包装到bash.sh中。 并且它在密码中等待的原因可能是因为您期望错误的模式,请尝试&#34;密码&#34;而不是&#34;密码&#34;并且在发送命令之后,期望终端模式以便scp完成

#!/bin/bash
while read p; do
echo $p
{
    /usr/bin/expect << EOF
    spawn scp $p user@192.168.4.151:/home/user/Desktop/
    expect "Password"
    send "123\r"
    expect "*#*"
EOF
}
done <List.txt
相关问题