2016-02-22 165 views
0

我有一個shell腳本batch_wrapper.sh,它接受用戶輸入並調用另一個腳本batch.sh。 這兩個腳本工作得很好。期望嵌套shell腳本

現在,我創建了一個期望腳本test.sh,它調用batch_wrapper.sh並提供輸入參數。 但是,出於某種原因,batch.sh腳本沒有被調用,我從期望腳本產生batch_wrapper.sh。我怎樣才能解決這個問題?

樣品預計腳本是:

#!/usr/bin/expect 

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

樣品Shell腳本:

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" 

回答

1

你的腳本立即退出戰後因爲沒有什麼可做的了。

您可以在expect腳本添加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