Linux
期望退出,不響應發送的文本
我有一個正在退出的期望腳本,而不是處理髮送的文本和安裝文件。我試圖弄清楚為什麼它沒有安裝文件,即如果我手動執行該過程而不是通過期望執行該過程,則沒有實現將要做什麼。
我的腳本中有以下程式碼:
#!/usr/bin/expect set exp_internal 1 set timeout 30 set java [lindex $argv 0]; set installer [lindex $argv 1]; spawn $java -jar $installer -console expect { "*More*" { send " "; exp_continue } "Press 1 to accept, 2 to reject, 3 to redisplay" { send "1\r" } } expect "Select the installation path:*" { send "/opt/pentaho8.3\r" } expect "Press 1 to continue, 2 to quit, 3 to redisplay\r\n" { send "1\r" } close exit
當我按如下方式執行腳本時:
$ sudo /usr/bin/expect -d install-pentaho.expect /usr/java/jdk1.8.0_261/bin/java pentaho-server-manual-ee-8.3.0.0-371/installer.jar
它按預期執行,除了最後幾行:
send: sending "/opt/pentaho8.3\r" to { exp6 } expect: does "" (spawn_id exp6) match glob pattern "Press 1 to continue, 2 to quit, 3 to redisplay\r\n"? no /opt/pentaho8.3 expect: does "/opt/pentaho8.3\r\r\n" (spawn_id exp6) match glob pattern "Press 1 to continue, 2 to quit, 3 to redisplay\r\n"? no Press 1 to continue, 2 to quit, 3 to redisplay expect: does "/opt/pentaho8.3\r\r\n\r\nPress 1 to continue, 2 to quit, 3 to redisplay\r\n" (spawn_id exp6) match glob pattern "Press 1 to continue, 2 to quit, 3 to redisplay\r\n"? yes expect: set expect_out(0,string) "Press 1 to continue, 2 to quit, 3 to redisplay\r\n" expect: set expect_out(spawn_id) "exp6" expect: set expect_out(buffer) "/opt/pentaho8.3\r\r\n\r\nPress 1 to continue, 2 to quit, 3 to redisplay\r\n" send: sending "1\r" to { exp6 }
任何人都知道為什麼期望發送的“1”被忽略?生成的程序似乎正在退出,忽略發送的“1”。
以下是預期的輸出,即我
sudo /usr/java/jdk1.8.0_261/bin/java -jar pentaho-server-manual-ee-8.3.0.0-371/installer.jar
手動執行 -console 時得到的輸出:Select the installation path: [/root/pentaho/pentaho-server-manual-ee-8.3.0.0-371] /opt/pentaho8.3 Press 1 to continue, 2 to quit, 3 to redisplay 1 ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── Installation ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── [ Starting to unpack ] [ Processing package: Base (1/1) ] [ Unpacking finished ] ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── Installation Finished ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── Installation was successful Application installed on /opt/pentaho8.3 [ Console installation done ]
關於為什麼沒有發生安裝以及期望腳本過早退出的任何想法?
問題是您正在關閉與 Java 程序的連接,因此當它寫入一些消息時,它會獲得 SIGPIPE 並退出。
更改
close
為expect eof
(如果安裝時間超過 30 秒,則可能調整超時)。這使得expect
等待程序完成(或至少直到它關閉標準輸出)。