我无法在expect循环中写入变量:
#!/bin/bash
expect -c "
set var1 10
puts {$var1}
puts $expect_out({$var1})
foreach name { bob jim jacobo henric } {
puts {hello $name $var1}
}"
我的输出是:
# ./test
({})
hello
hello
hello
hello
所以基本上它不会扩展任何变量。 有什么想法吗?
最佳答案
您应该用 '
引用,而不是 "
!这应该可以完成工作:
#!/bin/bash
expect -c '
set var1 10
puts "$var1"
send_user "Confirm? (Y/n): "
expect_user -re "(.*)\n"
set var1 $expect_out(1,string)
foreach name { bob jim jacobo henric } {
puts "hello $name $var1"
}
'
输出:
10
Confirm? (Y/n): Y
hello bob Y
hello jim Y
hello jacobo Y
hello henric Y
关于bash - Expect循环不打印变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42156302/