简体   繁体   English

在运行期望代码时发送错误

[英]send error while running expect code

I made an expect code: 我做了一个期望的代码:

#!/usr/bin/expect -f #!/ usr / bin / expect -f
spawn ls 产生ls
expect "*]$*" 期望“ *] $ *”
send "cd /to/some/path\\n" 发送“ cd / to / some / path \\ n”
expect "*]$*" 期望“ *] $ *”
send "sudo -u root ./program.sh" 发送“ sudo -u root ./program.sh”
expect "*:*" 期待“ *:*”
send "i_am_password\\n" 发送“ i_am_password \\ n”
interact 相互作用

while executing it I am getting the below error: 在执行它时,出现以下错误:

spawn ls 产生ls
my_pgm.exp abc.sh axyz.zip all.zip test.exp my_pgm.exp abc.sh axyz.zip all.zip test.exp
send: spawn id exp6 not open 发送:派生ID exp6未打开
while executing 在执行时
"send "cd /to/some/path\\n"" “发送“ cd / to / some / path \\ n”
(file "./my_pgm.exp" line 5) (文件“ ./my_pgm.exp”的第5行)

I am running this code on ubuntu. 我正在ubuntu上运行此代码。 Please help. 请帮忙。

I'm not sure to really understand why you need expect, but for a bash script, try this: 我不确定是否真正理解为什么需要期望,但是对于bash脚本,请尝试以下操作:

#!/usr/bin/expect -f
spawn bash -i
expect "*$ "
send "cd /to/some/path\n"
expect "*$ "
send "sudo -u root ./program.sh\n"
expect "*: "
send "i_am_password\n"
interact

Description 描述

The spawn directive instruct expect which program are to be used to interact with. spawn指令指示期望与 哪个程序进行交互。 So if you want to interact with bash, you have to ask expect to spawn bash . 因此,如果您与bash进行交互,则必须要求expect spawn bash The -i parameter to bash enforce them to start in interactive mode. bash的-i参数强制它们以交互方式启动。

At all, this look like a very bad idea , but that's only my opinion ;) 根本上, 这看起来是一个非常糟糕的主意 ,但这只是我的意见;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM