注意:您无法从屏幕会话中启动脚本工作方式。它会在会话中打开,没有标签......它更多的是相关提示,而不是问题的真正答案。
还有另一种解决方案,如果您接受通过运行进程进行屏幕会话...
新会话脚本
#!/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