我想實現一個期望腳本到bash腳本中。忍受着我,因爲我對bash/expect有點陌生。bash腳本內的期望
這裏是希望腳本按預期工作:
log_user 0
file delete foo.txt
set fh [open foo.txt a]
set servers {[email protected] [email protected]}
foreach s $servers {
spawn ssh $s
expect "password: "
send "PASSWORD\r"
expect "$ "
send "grep "something" /some/log/file.log"
expect "$ " { puts $fh "$expect_out(buffer)"}
send "exit\r"
}
close $fh
現在,我希望能在包括一個bash腳本這expect腳本但預期它不工作。
這是我到目前爲止有:
#!/bin/bash
XYZ=$(expect -c "
file delete foo.txt
set fh [open foo.txt a]
set servers {[email protected] [email protected]}
foreach s $servers {
spawn ssh $s
expect "password: "
send "PASSWORD\r"
expect "$ "
send "grep "something" /some/log/file.log"
expect "$ " { puts $fh "$expect_out(buffer)"}
send "exit\r"
}
close $fh
")
echo "$XYZ"
我得到的錯誤是:
command substitution: line 42: syntax error near unexpected token `('
command substitution: line 42: `expect "$ " { puts $fh "$expect_out(buffer)"}'
我接受任何其他方式來實現這個! :)
在您的原始腳本中,變量'$ servers'由Tcl擴展,因爲整個東西都是Tcl程序。在你的第二個腳本中,變量'$ server'由bash擴展,並且由於沒有帶這個名字的bash變量,它被替換爲空字符串。 – user1934428