Run a wait command inside a while loop in a shell script
I have a list of filenames in a text file, I need to transfer each file to the server using scp command. I am reading the filenames from Read.sh and passing each filename to the transfer. sh script, but scp does not execute the command in that transfer script. If I run transfer.sh alone with args passing its health.
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 password:
The next statement is not executed. Please suggest me any solution.
+3
source to share
1 answer
Try below script. The changes are that Transfer.sh is wrapped in bash.sh. and the reason it is waiting for a password might be because you are expecting a wrong pattern, try "Password" instead of "password" and after sending the command, wait for the terminal pattern for scp to terminate
#!/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
+5
source to share