从另一个脚本启动多个脚本化屏幕会话

发布于 2024-11-26 19:58:26 字数 447 浏览 1 评论 0原文

我编写了一个脚本(不起作用),看起来像这样:

#!/bin/sh

screen -dmS "somename" $HOME/somescript.sh

j=13
for i in {0..5}; do
    screen -dmS "name$i" $HOME/anotherscript.sh $i $j
    j=10
done

如果我将其复制并粘贴到终端中,它将创建 7 个独立的屏幕会话,正如我所期望的那样。但是,如果我从脚本中运行它,那么当我运行 screen -ls 时,我只会获得第一个会话“somename”。 我意识到 screen 可用于在一个会话中创建多个窗口。这些脚本如何运行对我来说并不重要。我只是想弄清楚为什么这不能作为脚本工作。

注意:我已经在 SuperUser 上问过这个问题,但没有任何合适的答复。我想也许这是一个错误的地方来问什么可以被视为编程问题。

I've written a script (that doesn't work) that looks something like this:

#!/bin/sh

screen -dmS "somename" $HOME/somescript.sh

j=13
for i in {0..5}; do
    screen -dmS "name$i" $HOME/anotherscript.sh $i $j
    j=10
done

If I copy and paste this into a terminal, it creates 7 detached screen sessions, as I expect. If I run it from within a script, however, I get only the first session, "somename," when I run screen -ls.
I realize screen can be used to create multiple windows within one session. It doesn't really matter to me how these scripts get run. I just want to get to the bottom of why this doesn't work as a script.

Note: I've asked this question on SuperUser without any suitable responses. I figured maybe that's the wrong place to ask what could be considered a programming question.

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

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

发布评论

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

评论(1

人心善变 2024-12-03 19:58:26

您可能会遇到的一件事是您正在运行的特定 shell 的特定版本。 /bin/sh 实际上可能是 bash,也可能是 bourne,这可能会影响循环语法的解释方式。例如,旧版本的 bash (v2.x) 和 bourne 都无法理解 {0..5} 构造(至少当我最终设法找到时) /bin/sh 这是一个真正的、实时的 bourne shell :-)。

我的建议是,如果您需要其语法,请将 shebang 行更改为 /bin/bash,并检查您的 bash 版本是否为 3.x 或更高版本。既然你说它可以从命令行运行,那么我的赌注是在 shebang 行。

One thing you might be getting bitten on is which specific version of which specific shell you're running. /bin/sh could actually be bash, or it could be bourne, and that can make a difference on how your loop syntax is interpreted. The {0..5} construct isn't understood in older versions of bash (v2.x), for instance, nor in bourne (at least it wasn't when I finally managed to track down a /bin/sh that was a real, live bourne shell :-).

My suggestion is to change your shebang line to /bin/bash if you need its syntax, and check that your bash is version 3.x or later. Since you say it works from the commandline, my bet is on the shebang line, though.

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