変化するコマンドプロンプトに入力を提供する方法を知りたいです。シェルスクリプトを使いたい
'#'が通常のプロンプトで、 '>'が私のプログラムに固有のプロンプトである例:
mypc:/home/usr1#
mypc:/home/usr1# myprogram
myprompt> command1
response1
myprompt> command2
response2
myprompt> exit
mypc:/home/usr1#
mypc:/home/usr1#
私が正しく理解していれば、特定のコマンドをプログラムにmyprogram
順番に送信したいと思うでしょう。
これを実現するには、簡単なexpect
スクリプトを使用できます。のプロンプトがmyprogram
で示されmyprompt>
、myprompt>
記号がresponse1
:に表示されないと仮定します。
#!/usr/bin/expect -f
#this is the process we monitor
spawn ./myprogram
#we wait until 'myprompt>' is displayed on screen
expect "myprompt>" {
#when this appears, we send the following input (\r is the ENTER key press)
send "command1\r"
}
#we wait until the 1st command is executed and 'myprompt>' is displayed again
expect "myprompt>" {
#same steps as before
send "command2\r"
}
#if we want to manually interract with our program, uncomment the following line.
#otherwise, the program will terminate once 'command2' is executed
#interact
起動myscript.expect
するには、スクリプトがと同じフォルダーにある場合に呼び出すだけmyprogram
です。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加