解决expect下命令不能解析通配符*的问题

使用expect可以省去手动交互的过程,比如scp的密码

曾遇到这样一段代码:(Bash脚本)

#!/usr/bin/expect -f
set HOST "192.168.102.1"
set USER "codefor"
set PASS "codefor"
set BOOKFILE "/home/codefor"

#upload
spawn "scp *.zip $USER@$HOST:$BOOKFILE"
expect {
    "*password*" {send "$PASS\r";}
}
expect eof;

这样的话,会报不存在*.zip的错误,即不能解析通配符*

在命令前加上bash -c即可。

如下:

#!/usr/bin/expect -f
set HOST "192.168.102.1"
set USER "codefor"
set PASS "codefor"
set BOOKFILE "/home/codefor"

#upload
spawn bash -c "scp *.zip $USER@$HOST:$BOOKFILE"
expect {
    "*password*" {send "$PASS\r";}
}
expect eof;

the End.

Codefor
posted @ 2011-07-21 10:43  Codefor  阅读(3522)  评论(0编辑  收藏  举报