[英]scp command not working inside expect in bash script
while [ $FileLine -le $FileListLines ];
do
# extract each line from FileList
str=$(tail -n+$FileLine ../$FileList | head -n1)
hostpath=$username@$ip:$str
export hostpath ip
expect -c '
spawn bash -c "scp -pr $env(hostpath) $env(ip)"
expect {
"(yes/no)?"{
send "yes\r"
expect "*?assword:*"
send "password\r"
}
"*?assword:*"{
send "password\r"
}
}
'
FileLine=$(( $FileLine + 1 ))
done
上面是bash腳本的一部分。 該scp
在命令expect
塊不工作,也就是從遠程計算機的文件沒有得到復制到本地機器。
從終端運行時,具有path
和hostname
的相同scp
命令可以正常工作。
在期望代碼的末尾添加expect eof
,否則在發送密碼后立即終止scp
進程。 (盡管不確定是否有問題,也請在模式和{
和{
expect {}
中的expect {}
之間)之間添加一個空格。)
expect -c '
spawn bash -c "scp -pr $env(hostpath) $env(ip)"
expect {
"(yes/no)?" {
send "yes\r"
expect "*?assword:*"
send "password\r"
}
"*?assword:*" {
send "password\r"
}
}
expect eof
'
剛剛嘗試過, "(yes/no)?"{
不起作用。 模式和{
之間的空格是必需的,因此應該為"(yes/no)?" {
"(yes/no)?" {
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.