期望脚本从文件读取命令 [英] Expect script reading commands from file

查看:236
本文介绍了期望脚本从文件读取命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个期望脚本,从文件中读取命令并执行它们,如。这是我的脚本 script.sh

I'm trying to write an expect script that reads commands from a file and executes them, as explained in this topic. Here is my script called script.sh:

#!/usr/bin/expect

set f [open "script_cmds.txt"]
set cmds [split [read $f] "\n"]
close $f

foreach cmd $cmds {
    spawn cmd 
}

expect eof
close

位于同一文件夹中的文件 script_cmds.txt 如下所示:

And the file script_cmds.txt, situated in the same folder, looks like this:

mkdir cmdlisttest1
mkdir cmdlisttest2

要在cygwin上编译和运行脚本,请使用

To compile and run the script on cygwin I use

chmod +x script.sh
d2u script.sh
./script.sh

p>

The output I get reads

spawn cmd
spawn cmd
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\cygwin\home\user>

然后停止而不退出。未创建文件夹 cmdlisttest1 cmdlisttest2

and then it stops there without exiting. The folders cmdlisttest1 and cmdlisttest2 are not created.

我相当新的期望脚本;


I'm fairly new to expect scripts; can anyone spot the mistake(s)?

推荐答案

您正在执行 spawn cmd 而不是 spawn $ cmd

使用 $ cmd 值存储在变量 cmd 中,这就是你想要的。

You're executing spawn cmd instead of spawn $cmd.
With $cmd you're reading the value stored in the variable cmd and that's what you want.

这篇关于期望脚本从文件读取命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆