将关键代码发送到 OS X 上的命令行程序
我想制作一个启动程序然后向其发送按键输入的脚本。在 psuedo-script 中:
#!/bin/bash
./program << (PRESS CONTROL-Z)
程序正在运行,因此如果脚本中有其他命令,则将无法访问它们,除非说 control-z 终止程序。
这可能吗?根据我的发现,我认为它可能需要密钥代码,但我可能是错的。
I want to make a script that starts a program and then sends it key input. In psuedo-script:
#!/bin/bash
./program << (PRESS CONTROL-Z)
The program is running so if there were additional commands in the script they will not be reached unless say control-z terminates the program.
Is this possible? From what I've found I thought it might require key codes but I could be wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为这可能是比“期望”更好的解决方案,因为它可以在本机 bash 脚本中执行,我有兴趣看看您的想法。
使用
注意反引号
例如,我编写了一个控制远程 gnu 屏幕会话的脚本,以下行打开窗口 2 并发出 ctrl-c 组合键
请参阅此处了解完整的代码参考。
要查找某些键输入的代码,您可以使用
最后一件事是上面提到的ascii代码参考列出了0x13作为回车符,但是在屏幕手册中他们列出了0x15作为回车键代码,有人知道为什么吗?我在本地屏幕上进行了测试,当我按 Enter 键时,会生成 0x13,但是当通过 ssh 向远程屏幕发送命令时,0x13 不起作用,但 0x15 起作用。
希望对
皮尔斯有帮助
This I think is probably a better solution than "expect" since it can be executed in native bash script, I'll be interested to see what you think.
Use
note the backticks
So for instance I have written a script that controls a remote gnu screen session, the following line opens window 2 and issues the ctrl-c key combo
See here for a complete reference of codes.
To find the code of some key input you can use
One last thing the ascii code reference mentioned above lists 0x13 as the carriage return, but in the screen manual they list 0x15 as the enter key code, does anyone know why? Ive tested in a local screen and when I press enter 0x13 is produced, but when sending commands via ssh to a remote screen 0x13 doesn't work but 0x15 does.
Hope that helps
Piers
您可能正在寻找
expect
(来自 http://expect.nist.gov/)。这涉及伪 tty 的复杂性,使程序看起来来自脚本(在本例中为expect
程序)的输入来自终端。或者,您可以使用
echo
或cat
并将其输出通过管道传输到程序中 - 这取决于程序。You might be looking for
expect
(from http://expect.nist.gov/). This deals with the complexities of pseudo-ttys that make it appear to the program that the input from the script (in this scenario, theexpect
program) is coming from a terminal.Alternatively, you might be able to use
echo
orcat
and pipe the output of that into the program - it depends on the program.如果您只想让程序在后台启动,只需执行以下操作
If you just want the program to start in the background, just do
如果您的意图是让程序后台运行,请使用:
编辑: 如果您的意图是停止程序(如 *nix shell 中的 ctrl-Z 经常做的那样),您可以向其发送 STOP 信号
:恢复执行,向其发送 CONT 信号:
在每个示例中,
pid
是程序的进程 ID。如果您在脚本中启动它,则很容易使用变量$!
来获取它,例如编辑 2: 如果您的程序是在收到 ctrl-Z 时退出的程序字符,然后记住控制字符具有字母表中位置字母的数值(即 Ctrl-A 为 1,Ctrl-B 为 2 等)。要将此字符发送到程序,您可以:(
032
是 26,即八进制的 ^Z。当然,您可以通过任何方式生成相同的字符,也许将其添加到其他输入的末尾就像( cat inputfile ; echo -e "\032" ) | ./prog
但这不一定有效;程序必须设计为从输入中识别该字符(它可能不会)。 t); 通常 shell 会捕获它,大多数从
stdin
读取输入的程序会在输入结束时退出,因此会重定向任何有限的输入(甚至是)。 code>) 应该会导致它终止。
最后,如果目的是在发生其他事件(在脚本中的其他地方检测到)时停止程序的执行,您可以
杀死
它。 ……If your intent is to background the program, use:
Edit: If your intent is to stop the program (as ctrl-Z often does in *nix shells), you can send it the STOP signal:
To resume the execution, send it the CONT signal:
In each of these examples
pid
is the process id of the program. If you launch it in a script, it's easy to get with the variable$!
, e.g.Edit 2: If your program is one that exits when it receives a ctrl-Z character, then remember that the control characters have the numerical value of the position letter in the alphabet (i.e. Ctrl-A is 1, Ctrl-B is 2, etc.). To send this character to a program you can:
(
032
is 26, i.e. ^Z, in octal. Of course you can produce the same character by any means, perhaps adding it to the end of other input like( cat inputfile ; echo -e "\032" ) | ./prog
.But this may not necessarily work; the program must be designed to recognise this character from the input (which it probably won't); usually the shell catches it. Then again, most programs reading input from
stdin
just exit when the input ends, so redirecting any finite input (even</dev/null
) should cause it to terminate.And, finally, if the intent was to stop the execution of the program when some other event (detected elsewhere in the script) has occurred, you can just
kill
it…