Linux 在 Screen 中,如何在单个屏幕会话中向所有虚拟终端窗口发送命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6510673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:48:38  来源:igfitidea点击:

in Screen, how do I send a command to all virtual terminal windows within a single screen session?

linuxgnu-screen

提问by Aaron Fi

I know how to create multiple windows within a single screen session, at startup:

我知道如何在启动时在单个屏幕会话中创建多个窗口:

But once I have them up and running, is there a way I can have my input be sent to all open windows, not just the one currently in focus?

但是一旦我启动并运行它们,有没有一种方法可以将我的输入发送到所有打开的窗口,而不仅仅是当前处于焦点的窗口?

采纳答案by Corey Henderson

I found a good tutorial here to do this:

我在这里找到了一个很好的教程来做到这一点:

http://blog.cone.be/2009/11/24/gnu-screen-nethack-different-screen-windows-sending-commands-to-all-screen-windows/

http://blog.cone.be/2009/11/24/gnu-screen-nethack-different-screen-windows-sending-commands-to-all-screen-windows/

From the post:

从帖子:

Once you re used to the multiple windows, you might run into a situation where you want to send a same command to several of these open windows. Screen provides in the “at” command to do this. First you ll need to open command line mode.

C-a : (colon) Enter command line mode.

This way you can type a command once, but you ll still have to enter each separate window. But there is a better way. As an example we ‘ll send “ls -l” to all the windows.

at "#" stuff "ls -l^M"

This command is barely readable, so let's pick it apart! The first part is 'at [identifier][#|*|%] command'. The at command sends the text parameter to all the windows you specified in the identifier. You can match the criteria to either the window name or number with #, the user name with * or the displays, using %. The next part is the command you want to run in the selected windows. We're using "stuff" to stuff the command we want to execute into the input buffer of the selected windows. Stuff is really straightforward. It simply stuffs the string you gave as a parameter. Next problem is the command. Or rather having it executed! To get screen to put an “enter” after the command, to execute the command, add “^M” at the end. You can do a lot more with this than just sending an ls to the input. Any screen command, like renaming, moving windows around, whatnot .. is available in combination with "at".

一旦您习惯了多个窗口,您可能会遇到想要向其中多个打开的窗口发送相同命令的情况。Screen 中提供了“at”命令来执行此操作。首先,您需要打开命令行模式。

Ca:(冒号)进入命令行模式。

这样您就可以输入一次命令,但您仍然必须输入每个单独的窗口。但是有更好的方法。例如,我们将向所有窗口发送“ls -l”。

在“#”的东西“ls -l^M”

这个命令几乎不可读,所以让我们把它拆开!第一部分是“at [identifier][#|*|%] 命令”。at 命令将文本参数发送到您在标识符中指定的所有窗口。您可以将条件与带有 # 的窗口名称或编号、带有 * 的用户名或使用 % 的显示相匹配。下一部分是您要在所选窗口中运行的命令。我们使用“stuff”将我们想要执行的命令填充到所选窗口的输入缓冲区中。东西真的很简单。它只是填充您作为参数提供的字符串。下一个问题是命令。或者更确切地说是执行它!要让屏幕在命令后加上“回车”,要执行命令,请在末尾添加“^M”。除了向输入发送 ls 之外,您还可以用它做更多的事情。任何屏幕命令,

回答by minaev

Sorry for this belated reply, but tmuxmight be a better choice for you than screen. In tmux, you have to press: C-b :to enter the command mode and input: setw synchronize-panes(or just setw sync<Tab>using autocompletion). Note that this command enables synchronization between panes (areas of one split screen, visible simultaneously), but not between windows (full, not split, screens).

很抱歉这个迟来的回复,但tmux对您来说可能是比 更好的选择screen。在 中tmux,您必须按:C-b :进入命令模式并输入:(setw synchronize-panes或仅setw sync<Tab>使用自动完成)。请注意,此命令启用窗格之间的同步(一个分屏的区域,同时可见),但不能在窗口之间(完整的,不分屏的,屏幕)。

回答by TipTop

You may also want to send to selected windows only (multicast).

您可能还想仅发送到选定的窗口(多播)。

  1. Convention: Give 1st character of window title the meaning of a broadcast flag. E.g. if title starts with "." then the window listens to broadcast, otherwise it doesn't.
  2. Set titles as needed.

    screen -S SessionName -p 0 -X title "remote_0"     # window 0: ignore multicast
    screen -S SessionName -p 1 -X title ".remote_1"    # window 1: listen to multicast
    
  3. Send contentsto listeners with the at command of screen

    screen -S SessionName -X at ".#" stuff "date
    "
    
  4. Note: the ending double quote above ensures a ^M to be sent (Cr).

  5. You may enable / disable multicast based on window title this way.

  1. 约定:给窗口标题的第一个字符一个广播标志的含义。例如,如果标题以“.”开头。然后窗口监听广播,否则不监听。
  2. 根据需要设置标题。

    screen -S SessionName -p 0 -X title "remote_0"     # window 0: ignore multicast
    screen -S SessionName -p 1 -X title ".remote_1"    # window 1: listen to multicast
    
  3. 使用屏幕的 at 命令将内容发送给听众

    screen -S SessionName -X at ".#" stuff "date
    "
    
  4. 注意:上面的结尾双引号确保发送 ^M (Cr)。

  5. 您可以通过这种方式根据窗口标题启用/禁用多播。