Expect脚本不能从文件中读取mkdir并执行 [英] Expect script not able to read mkdir from file and execute

查看:1148
本文介绍了Expect脚本不能从文件中读取mkdir并执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是此主题的续篇;因为它是一个不同的问题从OP我正在做一个新的话题。我有一个期望脚本我不能得到工作,应该从一个文件中读取命令并执行它们。这是脚本本身,称为 script.sh

This is a continuation of this topic; since it's a different question from the OP I'm making a new topic. I have an expect script I can't get working that is supposed to read commands from a file and execute them. This is the script itself, called script.sh:

#!/usr/bin/expect

set prompt {\$\s*$}
set f [open "script_cmds_u.txt"]
set cmds [split [read $f] "\n"]
close $f

foreach cmd $cmds {
    spawn $cmd
    expect -re $prompt
}

expect eof
close

文件script_cmds.txt像这样:

The file script_cmds.txt looks like this:

mkdir cmdlisttest1
mkdir cmdlisttest2

运行脚本我使用

tr -d '\r' < testpswd2.sh > testpswd2_u.sh
tr -d '\r' < script_cmds.txt > script_cmds_u.txt
chmod +x testpswd2_u.sh
./testpswd2_u.sh

这样做我得到以下错误:无法执行mkdir cmdlisttest1:没有这样的文件或目录

Doing it like this I get the following error: couldn't execute "mkdir cmdlisttest1": no such file or directory.

所以我尝试了一些变化:

So I try a number of changes:


  • 将它改为 send $ cmd 在循环中。这会将输出更改为 mkdir cmdlisttest1couldn't compile正则表达式模式:invalid escape \ sequence 。我还可以在 script_cmds中的命令之后使用 \\\
    \r txt

  • 在循环中删除 expect -re $ prompt 。然后我得到 mkdir cmdlisttest1mkdir cmdlisttest2 ,它挂在那里。

  • 放一个 send $ cmd 后面的然后我得到

  • Change it to send $cmd in the loop. This changes the output to mkdir cmdlisttest1couldn't compile regular expression pattern: invalid escape \ sequence. I also try this with \n and \r after the commands in script_cmds.txt.
  • Remove the expect -re $prompt in the loop. Then I get mkdir cmdlisttest1mkdir cmdlisttest2 and it hangs there.
  • Put a spawn "\n" after the send $cmd; then I get

mkdir cmdlisttest1spawn

无法执行

:无此类文件或目录

推荐答案

错误是由于 将不带参数的整个字符串(例如: mkdir cmdlisttest1 )作为命令。

The error is due to spawn is taking the whole string (e.g.: mkdir cmdlisttest1) as a command without arguments.

改用参数展开(感谢 @glenn jackman ):

foreach cmd $cmds {
    spawn {*}$cmd
    expect -re $prompt
}






另一种选择:


Another option:

foreach cmd $cmds {
    spawn [lindex $cmd 0] [lrange $cmd 1 [llength $cmd]]
    expect -re $prompt
}

使用 [lindex $ cmd 0] 您将获得 mkdir ;

使用 [lrange $ cmd 1 [llength $ cmd]] ,参数 cmdlisttest1 )。

With [lindex $cmd 0] you'll get mkdir;
With [lrange $cmd 1 [llength $cmd]], the arguments (e.g. cmdlisttest1).


lindex 列表索引

返回索引项目从列表。注意:列表从0开始,而不是1,因此第一个项目在索引0,第二个项目在索引1,依此类推。

lindex list index
Returns the index'th item from the list. Note: lists start from 0, not 1, so the first item is at index 0, the second item is at index 1, and so on.

lrange 最后列表

返回由列表中第一个到最后一个条目组成的列表。如果first小于或等于0,则将其视为第一个列表元素。 如果last是end或大于列表中元素数量的值,则将其视为结束。如果first大于last,则返回空列表。

lrange list first last
Returns a list composed of the first through last entries in the list. If first is less than or equal to 0, it is treated as the first list element. If last is end or a value greater than the number of elements in the list, it is treated as the end. If first is greater than last then an empty list is returned.

这篇关于Expect脚本不能从文件中读取mkdir并执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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