简体   繁体   English

期望与嵌套的shell脚本

[英]expect with nested shell script

I have a shell script batch_wrapper.sh which takes user input and it invokes another script batch.sh. 我有一个shell脚本batch_wrapper.sh,它接受用户输入,并调用另一个脚本batch.sh。 These two script work perfectly fine. 这两个脚本工作得很好。

Now, I have created a expect script test.sh which invokes batch_wrapper.sh and provides the input parameter. 现在,我创建了一个期望脚本test.sh,该脚本调用batch_wrapper.sh并提供输入参数。 However, for some reason, batch.sh script is not getting invoked which I spawn batch_wrapper.sh from expect script. 但是,由于某种原因,未调用batch.sh脚本,我从期望脚本中生成了batch_wrapper.sh。 How can I fix this? 我怎样才能解决这个问题?

Sample expect script is: 样本期望脚本为:

#!/usr/bin/expect

set timeout 2000
spawn "./batch_wrapper.sh"
expect "username" {send "Vikas\r" }

Sample Shell Scripts are: 示例Shell脚本为:

batch_wrapper.sh batch_wrapper.sh

#!/bin/bash

echo "enter username"
read name
echo "your name is $name"
./batch.sh

batch.sh 批处理

#!/bin/bash

echo "Inside batch"
echo "exiting batch"

Your script exits immediately afterwards as there's nothing left to do. 您的脚本随后将立即退出,因为没有任何事情要做。

You can add interact or expect eof {} as the last line in your expect script to make it process the remainding output: 您可以在期望脚本的最后一行中添加interactexpect eof {}作为最后一行,以使其处理剩余的输出:

$ cat lol.expect 
#!/usr/bin/expect

set timeout 2000
spawn "./batch_wrapper.sh"
expect "username" {send "Vikas\r" }
expect eof {}

$ expect -f lol.expect 
spawn ./batch_wrapper.sh
enter username
Vikas
your name is Vikas
Inside batch
exiting batch

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

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