Bash 在shell脚本中循环时在内部运行expect命令

Bash 在shell脚本中循环时在内部运行expect命令,bash,shell,scripting,expect,scp,Bash,Shell,Scripting,Expect,Scp,我有一个文本文件中的文件名列表,需要使用scp命令将每个文件传输到服务器。我正在从Read.sh读取文件名,并将每个文件名传递给transfer.sh脚本,但scp没有在此传输脚本中执行命令。如果我单独运行transfer.sh并传递参数,则其工作正常 List.txt /home/kittu/file1.txt /home/kittu/file2.txt /home/kittu/file3.txt Read.sh #!/bin/bash while read p; do echo $p

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

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
#!/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:

它不会执行下一个语句。请向我建议任何解决方案。

尝试下面的脚本,更改是Transfer.sh被包装到bash.sh中。 它在密码中等待的原因可能是因为您预期的模式错误,请尝试“password”而不是“password”,在发送命令后,预期的是终端模式,以便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
#/bin/bash
读p;做
回声$p
{

/usr/bin/expect您是否尝试独立运行transfer.sh,它是否工作?在transfer.sh中,interact命令应该抛出错误,因为您尝试与关闭的进程通信(在本例中为scp)@Ram是的,传输以独立方式工作。感谢您的评论,我在这里尝试了上述代码,我收到了sytax错误和警告:此处文档第4行由文件结尾分隔(通缉“EOF”)语法错误:意外的文件结尾问题是由于复制粘贴错误。您必须删除每行开头的额外空格,请尝试此命令“sed-i's//^*//g'filename”通过脚本并重新运行脚本,抱歉格式错误嘿,我刚刚删除了echo$p,它正在工作。感谢您的回答。是否可以在while循环中删除与$p匹配的行。因为我想在文件传输到服务器后从List.txt中删除文件名。??是否参考该行“生成scp$puser@192.168.4.151:/home/user/Desktop/“?