输入到 shell 脚本中的专有命令提示符



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

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

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 "command1r"
}
#we wait until the 1st command is executed and 'myprompt>' is displayed again
expect "myprompt>" {
#same steps as before
send "command2r"
}
#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位于同一文件夹中。

鉴于myprogram是一个脚本,它必须提示输入类似while read IT; do ...something with $IT ...;done的内容。很难说出如何在没有看到的情况下更改该脚本。echo -n 'myprompt>将是最简单的补充。

可以用PS3select构造来完成

#!/bin/bash
PS3='myprompt> '
select cmd in command1 command2
do
case $REPLY in
command1)
echo response1
;;
command2)
echo response2
;;
exit)
break
;;
esac
done

或者内置echoread

prompt='myprompt> '
while [[ $cmd != exit ]]; do
echo -n "$prompt"
read cmd
echo ${cmd/#command/response}
done

相关内容

  • 没有找到相关文章

最新更新