运行预计shell脚本while循环中的命令 [英] Run expect command inside while loop in shell script
问题描述
我有一个文本文件的文件名列表,需要使用scp command.I正在读从 Read.sh 并传递每个文件名,文件名设置为传递的每个文件传输到服务器。在这种转移SH 脚本,但SCP不执行命令script.If我跑的 transfer.sh 独自路过的ARG其工作的罚款。
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
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为
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:
它不执行下一个statement.Please建议我任何解决方案。
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\r"
expect "*#*"
EOF
}
done <List.txt
这篇关于运行预计shell脚本while循环中的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!