我尝试在 shell 脚本中执行以下操作:
bash;
在 bash 上下文中:
- 跑
./a.out
在./a.out上下文中,我需要模拟按键:
yes
3292
no
我该怎么做?我所有的尝试都失败了&,&&并且;在主 shell 上下文中而不是在 bash 中执行后续命令。
bash && echo "yes" > /dev/console
我已经看到在 shell 脚本中使用期望没有使用本机 shell 命令的任何替代方法吗?我不想依赖其他工具。
我尝试在 shell 脚本中执行以下操作:
bash;
在 bash 上下文中:
./a.out在./a.out上下文中,我需要模拟按键:
yes
3292
no
我该怎么做?我所有的尝试都失败了&,&&并且;在主 shell 上下文中而不是在 bash 中执行后续命令。
bash && echo "yes" > /dev/console
我已经看到在 shell 脚本中使用期望没有使用本机 shell 命令的任何替代方法吗?我不想依赖其他工具。
看看expect,它在用户提供的脚本的帮助下与交互式程序“对话”。
用法
expect ./interact
或制作interact可执行文件(chmod a+x interact):
./interact
interact以下脚本在哪里:
#!/usr/bin/expect
spawn ./a.out
send -- "yes\r"
expect "3292\r"
send -- "no\r"
这只是一个简单的示例,手册页充满了深入的解释,并且还有安装附带的示例脚本。
参考