Bash

從 bash 向 python 腳本發送“輸入密鑰”

  • March 27, 2017

我正在嘗試使用 bash 腳本自動化我的 ubuntu 設置並遇到以下問題:

我希望腳本在執行時自動發送enter擊鍵umake ide eclipse(這會從終端安裝 eclipse ide)。

這是標準輸出,在沒有腳本的情況下從終端執行時:

$ umake ide eclipse
Choose installation path: /home/gn4i/.local/share/umake/ide/eclipse
<need to press enter>
Downloading and installing requirements      

通常我會這樣做echo | umake ide eclipse,但我總是收到以下錯誤

$ echo | umake ide eclipse
Choose installation path: Choose installation path: ERROR: Unhandled exception
Traceback (most recent call last):
 File "/usr/lib/python3/dist-packages/umake/tools.py", line 158, in wrapper
   function(*args, **kwargs)
 File "/usr/lib/python3/dist-packages/umake/ui/__init__.py", line 42, in display
   cls.currentUI._display(contentType)
 File "/usr/lib/python3/dist-packages/umake/ui/cli/__init__.py", line 61, in _display
   contentType.run_callback(result=rlinput(contentType.content, contentType.default_input))
 File "/usr/lib/python3/dist-packages/umake/ui/cli/__init__.py", line 41, in rlinput
   return input(prompt + " ")
EOFError: EOF when reading a line

如何自動化此安裝?

我設法用螢幕方法解決了它。這在後台執行,我看不到進度,但這對我來說沒問題

screen -d -m -S umake-eclipse
screen -S umake-eclipse -p 0 -X stuff "umake ide eclipse\n\n"

使用一個非常簡單的expect腳本:

spawn umake ide eclipse
expect "Choose installation path:" { sleep 1; send "\r" }

執行它:

$ expect -f script.expect

引用自:https://unix.stackexchange.com/questions/338902