将关键代码发送到 OS X 上的命令行程序

发布于 2024-08-28 14:10:50 字数 223 浏览 7 评论 0原文

我想制作一个启动程序然后向其发送按键输入的脚本。在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

水波映月 2024-09-04 14:10:50

我认为这可能是比“期望”更好的解决方案,因为它可以在本机 bash 脚本中执行,我有兴趣看看您的想法。

使用

`printf "character code here"` 

注意反引号

例如,我编写了一个控制远程 gnu 屏幕会话的脚本,以下行打开窗口 2 并发出 ctrl-c 组合键

ssh -t user@$host screen -p 2 -X stuff `printf "\003"`
  • -t 选项模拟远程计算机上的终端输入
  • -p 允许我们指定我们在屏幕会话中连接到的窗口的名称或编号。
  • \003 是字符代码 0x03 的 bash 格式,

请参阅此处了解完整的代码参考。

要查找某些键输入的代码,您可以使用

printf "%#x\n" "'X"
0x58
  • 如果 X 是您要查找代码的键
  • 要查找非文字的代码,您可以使用 ctrl-v (使 bash 将下一个键附加到命令行而不是解释它),然后键入组合键,因此,如果我想查找 ctrl-c 的键代码,我会删除 X 按 ctrl-v,然后按 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

`printf "character code here"` 

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

ssh -t user@$host screen -p 2 -X stuff `printf "\003"`
  • The -t option simulates terminal input on the remote machine
  • -p allows us to specify the name or number of the window we are connecting to within the screen session.
  • \003 is the bash format of character code 0x03

See here for a complete reference of codes.

To find the code of some key input you can use

printf "%#x\n" "'X"
0x58
  • Were X is the key you want to find the code of
  • To find codes of non literals you can use ctrl-v (makes bash append the next key to the command line rather than intepret it) and then type the key combo, so if I wanted to find the key code for ctrl-c I would delete the X press ctrl-v and then press ctrl-c.

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

无远思近则忧 2024-09-04 14:10:50

您可能正在寻找 expect (来自 http://expect.nist.gov/)。这涉及伪 tty 的复杂性,使程序看起来来自脚本(在本例中为 expect 程序)的输入来自终端。

或者,您可以使用 echocat 并将其输出通过管道传输到程序中 - 这取决于程序。

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, the expect program) is coming from a terminal.

Alternatively, you might be able to use echo or cat and pipe the output of that into the program - it depends on the program.

北座城市 2024-09-04 14:10:50

如果您只想让程序在后台启动,只需执行以下操作

#!/bin/bash
./program&

If you just want the program to start in the background, just do

#!/bin/bash
./program&
千柳 2024-09-04 14:10:50

如果您的意图是让程序后台运行,请使用:

./program &    # The & sends the command to the background
echo commands here are executed while program is in the background
…
wait           # Wait for the completion of background commands
echo commands here are executed after background program has completed

编辑: 如果您的意图是停止程序(如 *nix shell 中的 ctrl-Z 经常做的那样),您可以向其发送 STOP 信号

kill -STOP pid

:恢复执行,向其发送 CONT 信号:

kill -CONT pid

在每个示例中,pid 是程序的进程 ID。如果您在脚本中启动它,则很容易使用变量 $! 来获取它,例如

./prog &
echo prog started in the background
pid_of_prog=$!
kill -STOP $pid_of_prog
echo prog stopped
kill -CONT $pid_of_prog
echo prog continues
wait
echo prog finished

编辑 2: 如果您的程序是在收到 ctrl-Z 时退出的程序字符,然后记住控制字符具有字母表中位置字母的数值(即 Ctrl-A 为 1,Ctrl-B 为 2 等)。要将此字符发送到程序,您可以:(

echo -e "\032" | ./prog

032 是 26,即八进制的 ^Z。当然,您可以通过任何方式生成相同的字符,也许将其添加到其他输入的末尾就像 ( cat inputfile ; echo -e "\032" ) | ./prog

但这不一定有效;程序必须设计为从输入中识别该字符(它可能不会)。 t); 通常 shell 会捕获它,大多数从 stdin 读取输入的程序会在输入结束时退出,因此会重定向任何有限的输入(甚至是 )。 code>) 应该会导致它终止。

最后,如果目的是在发生其他事件(在脚本中的其他地方检测到)时停止程序的执行,您可以杀死它。 ……

If your intent is to background the program, use:

./program &    # The & sends the command to the background
echo commands here are executed while program is in the background
…
wait           # Wait for the completion of background commands
echo commands here are executed after background program has completed

Edit: If your intent is to stop the program (as ctrl-Z often does in *nix shells), you can send it the STOP signal:

kill -STOP pid

To resume the execution, send it the CONT signal:

kill -CONT pid

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.

./prog &
echo prog started in the background
pid_of_prog=$!
kill -STOP $pid_of_prog
echo prog stopped
kill -CONT $pid_of_prog
echo prog continues
wait
echo prog finished

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:

echo -e "\032" | ./prog

(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…

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文