简体   繁体   中英

send error while running expect code

I made an expect code:

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

while executing it I am getting the below error:

spawn ls
my_pgm.exp abc.sh axyz.zip all.zip test.exp
send: spawn id exp6 not open
while executing
"send "cd /to/some/path\\n""
(file "./my_pgm.exp" line 5)

I am running this code on ubuntu. Please help.

I'm not sure to really understand why you need expect, but for a bash script, try this:

#!/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. So if you want to interact with bash, you have to ask expect to spawn bash . The -i parameter to bash enforce them to start in interactive mode.

At all, this look like a very bad idea , but that's only my opinion ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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