bash Shell 脚本用多个窗口启动 1 个会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23418537/
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
Shell script start 1 session with multiple windows
提问by user3578925
I am pretty new to shell scripting (you may say I am just starting). What I need is to write a shell script to open ONLY 1 "screen" session. Then I want to open multiple windows (say 10) in the same session and have each session to do something e.g print "hello". So here is a part of my code but it only creates one window (0) and doesn't print anything on that window:
我对 shell 脚本很陌生(你可能会说我才刚刚开始)。我需要的是编写一个 shell 脚本来打开只有 1 个“屏幕”会话。然后我想在同一个会话中打开多个窗口(比如 10 个),并让每个会话做一些事情,例如打印“你好”。所以这是我的代码的一部分,但它只创建一个窗口 (0) 并且不会在该窗口上打印任何内容:
#!/bin/bash
screen-d -m -S mysession
for n in {1..10}; do
i=$(($n-1))
screen -S mysession -p $i -X echo "hello"
done
Like I said, my sample code doesn't work! It opens one session with only one window '0', and there is nothing printed on the terminal on window '0'.
就像我说的,我的示例代码不起作用!它打开一个只有一个窗口“0”的会话,并且终端上的窗口“0”上没有打印任何内容。
Could you please give me some help? The code is supposed to open one screen session and then in the loop to open 10 windows and print "hello" in each window.
你能给我一些帮助吗?该代码应该打开一个屏幕会话,然后在循环中打开 10 个窗口并在每个窗口中打印“hello”。
Thank you in advance!
先感谢您!
Abedin
阿贝丁
回答by ymonad
The command you can send with -X
option is not shell command but screen command.
您可以使用-X
option发送的命令不是 shell 命令,而是 screen 命令。
Check CUSTOMIZATION section in man screen
to see the list of screen command.
the following code uses screen
command to create new window and stuff
command to show text on the window.
检查 CUSTOMIZATION 部分man screen
以查看屏幕命令列表。以下代码使用screen
命令创建新窗口和stuff
命令在窗口上显示文本。
#!/bin/bash
screen -d -m -S mysession
# window 0 is created by default, show hello0 on it
screen -S mysession -p 0 -X stuff hello0
for n in {1..9}; do
# create now window using `screen` command
screen -S mysession -X screen $n
screen -S mysession -p $n -X stuff hello$n
done
Now you can attach to myscreen
session and check that there are 10 windows and hello0 .. hello9 is displayed in each window.
现在您可以附加到myscreen
会话并检查是否有 10 个窗口和 hello0 .. hello9 显示在每个窗口中。
$ screen -r mysession
[Press C-a "]