在 shell 脚本中的 while 循环中运行 expect 命令 [英] Run expect command inside while loop in shell script

查看:67
本文介绍了在 shell 脚本中的 while 循环中运行 expect 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have list of filenames in a text file,need to transfer each file into server using scp command.I am reading filenames from Read.sh and passing each file name to transfer.sh script but scp is not executing command in this transfer script.If I run transfer.sh alone with passing args its working fine.

List.txt

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

读取.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
"
interact

我只是将 Read.sh 运行为

I just run Read.sh as

>./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:

它没有执行下一条语句.请建议我任何解决方案.

Its not executing next statement.Please suggest me any solution.

推荐答案

试试下面的脚本,变化是 Transfer.sh 被包装到 bash.sh 中.它在密码中等待的原因可能是因为您期待错误的模式,请尝试密码"而不是密码",并在发送命令后,期待终端模式,以便 scp 完成

Try the below script , The changes are that the Transfer.sh is wrapped into bash.sh. and the reason it waits in the password may be because you are expecting a wrong pattern , try "Password" instead of "password" and after send command, expect for the terminal pattern so that the scp finishes

#!/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
"
    expect "*#*"
EOF
}
done <List.txt

这篇关于在 shell 脚本中的 while 循环中运行 expect 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆