Run expect command inside while loop in shell script

匿名 (未验证) 提交于 2019-12-03 07:36:14

问题:

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 

I just run Read.sh as

>./Read.sh 

Output:

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

回答1:

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 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!