1

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

我需要这样的脚本:

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

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

4

2 回答 2

3

注意:您无法从屏幕会话中启动脚本工作方式。它会在会话中打开,没有标签......它更多的是相关提示,而不是问题的真正答案。

还有另一种解决方案,如果您接受通过运行进程进行屏幕会话...

新会话脚本

#!/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.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

于 2010-02-12T16:27:44.090 回答
1

在屏幕内运行此脚本可以满足我的要求:

#!/bin/bash

screen vi
screen top
于 2010-01-28T17:32:25.763 回答