如何在屏幕会话中编写 gnu screen 脚本来打开新窗口并在其中运行命令?

发布于 2024-08-19 16:02:07 字数 327 浏览 1 评论 0原文

在屏幕会话中,我想运行一个打开的 shell 脚本 在同一会话中创建一些新屏幕窗口并开始运行一些 其中的程序。

我需要这样的脚本:

screen -t newWindow
[switch to newWindow and execute a command]
screen -t newWindow2
[switch to newWindow2 and execute a command]

我不知道如何实现我在括号中描述的效果。 有什么线索吗?请注意,这不是我将运行来启动屏幕会话的脚本。我需要这个脚本可以在现有的屏幕会话中运行,以便向会话添加新窗口。

From within a screen session, I'd like to run a shell script that opens
a few new screen windows in the same session and start running some
programs in them.

I need a script like this:

screen -t newWindow
[switch to newWindow and execute a command]
screen -t newWindow2
[switch to newWindow2 and execute a command]

I don't know how to accomplish the effect I describe in the brackets.
Any clues? Please note that this is not a script I'll be running to start a screen session. I need this script to be runnable within an existing screen session, in order to add new windows to the session.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

瀟灑尐姊 2024-08-26 16:02:07

注意:您无法从屏幕会话中启动按以下方式工作的脚本。并且它不会在会话中打开任何选项卡...它更多的是相关提示,而不是问题的真正答案。

还有其他解决方案,如果您接受通过运行进程来进行屏幕会话...

新会话script

#!/bin/sh
echo "nouvelle session screen ${sessionName}"
screen -S ${sessionName}  init.sh
echo "screen session: done"
echo "go to ${AnyWhere}"
sleep 1
screenexec ${sessionName} "cd ${AnyWhere}"

init 脚本(此处为“init.sh”)

#!/bin/zsh
zsh -c "sleep 0.2"
screen -d #detach the initialised screen
zsh       #let a prompt running

注入脚本(此处为 screenexec)

#!/bin/sh
# $1 -> nom de screen cible  $2 -> commande
echo "injection de «${2}» dans la session «${1}» ..."
screen -x "$1" -X stuff "$2"              #inject the command
screen -x "$1" -X eval "stuff \015"       #inject \n
echo "Done"

通过使用这种方式,您应该可以轻松地在屏幕中注入代码,如果您的脚本像守护进程一样运行,那么这很有趣......

对于那些喜欢使用 python 脚本的人来说,我制作了一个小库来创建会话、关闭会话、注入命令: ScreenUtils.py

这是一个小项目,不处理多窗口屏幕会话。

忘了说我很久以前就用它制作了一个真正的Python库: https://github.com/Christophe31/screenutils

Note: you can't launch script working following way from a screen session. And it will open in session no tabs... Its more a related tip than a real answer to the question.

There is an other solution, if you accept to have a screen session by running process...

new session script

#!/bin/sh
echo "nouvelle session screen ${sessionName}"
screen -S ${sessionName}  init.sh
echo "screen session: done"
echo "go to ${AnyWhere}"
sleep 1
screenexec ${sessionName} "cd ${AnyWhere}"

init script (here "init.sh")

#!/bin/zsh
zsh -c "sleep 0.2"
screen -d #detach the initialised screen
zsh       #let a prompt running

injection script (here screenexec)

#!/bin/sh
# $1 -> nom de screen cible  $2 -> commande
echo "injection de «${2}» dans la session «${1}» ..."
screen -x "$1" -X stuff "$2"              #inject the command
screen -x "$1" -X eval "stuff \015"       #inject \n
echo "Done"

By using this way, you should inject code easily in your screens, interesting if your script act like a deamon...

For those who prefer script in python, I've made a small lib to create sessions, close sessions, inject commands: ScreenUtils.py

It's a small project, which don't handle multiwindows screen sessions.

Forgot to mention I made a real python library out of it long ago: https://github.com/Christophe31/screenutils

萌能量女王 2024-08-26 16:02:07

在屏幕内运行此脚本可以实现我认为您想要的功能:

#!/bin/bash

screen vi
screen top

Running this script inside screen does what I think you want:

#!/bin/bash

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