expect中使用exec执行shell命令的方法

来源:互联网 发布:淘宝店怎么才能有生意 编辑:程序博客网 时间:2024/05/18 02:02

今天想在expect脚本中获取本机ip,执行脚本是报错,脚本如下:

set localip [exec ifconfig eth0 | grep Mask | cut -d: -f2 | awk '{print $1}]

google了一下,有些文章说需要对shell命令进行修改替换,但我试了一下,依然报错。

继续google,终于发现了一个很简单的解决方法,命令如下:

exec sh -c {your commands here}。

 获取ip的脚本可以写成:

set idcid [exec sh -c {ifconfig eth0 | grep Mask | cut -d: -f2 | awk '{print $1}'}]


参考资料:

1. http://www.linuxquestions.org/questions/linux-software-2/ksh-tcl-173092/

2. http://blog.csdn.net/bonny95/article/details/5786283

原创粉丝点击