在外壳脚本中输入专有命令提示符 [英] Input to proprietary command prompt in shell script

查看:26
本文介绍了在外壳脚本中输入专有命令提示符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何为更改的命令提示符提供输入。我想使用外壳脚本

示例,其中‘#’是通常的提示,‘>’是特定于我的程序的提示:

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 (
 is the ENTER key press)
    send "command1
"
}

#we wait until the 1st command is executed and 'myprompt>' is displayed again
expect "myprompt>" {
    #same steps as before
    send "command2
"
}

#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位于同一文件夹中。

这篇关于在外壳脚本中输入专有命令提示符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆