我想打开一个新的终端选项卡,作为我正在处理的项目的一部分,要求我在运行第一部分后在新选项卡中运行另一个脚本。但我不知道如何在 macOS Big Sur 上自动执行此操作。我认为 AppleScript 是可行的方法,但如果有另一种方法可以从 ZSH 运行,我会很兴奋。
TLDR;我不知道这是可能的,但在 bash/zsh 中,所有产生多个后台进程的终端恶作剧都是可能的。请参阅下面的示例以及链接。
无法从终端本身在 Vscode 内部创建新终端。 Jupyterlab 也是如此,主要是出于安全原因。但可以生成另一个终端作为原始命令的一部分。
bash -c 'some string command'
以及使用
&
;
&&
和 ||
等运算符,它们根据某些条件执行。 以执行为主,不要等待。不管第一个命令成功;
都执行,后两个命令成功和不成功都执行。 (在这里查看更多)
command1 & command2
示例:
echo 'something1' & echo 'something2'
产生以下结果:
[1] 53159
something1
something2
[1] + done echo 'something1'
这可以与第一个示例结合使用:
bash -c 'sleep 3; echo "something1"' & echo "something2"
,生成:
[1] 60427
something2
<machine_id>% something1
[1] + done bash -c 'sleep 3; echo "something1"'
此外,您可能可以包括一些等待和信号捕获,但这是另一种蠕虫病毒。
我将
VSCode
视为关键字。不是 zsh
或 Apple
。
由于这是您要使用的主要环境,因此您应该在这个环境中寻找答案。
而
Tasks
功能就是答案:https://code.visualstudio.com/Docs/editor/tasks。
我强烈建议您熟悉它。它的功能比我在这个答案中展示的功能要多。
<task>.presentation.panel
参数负责指定面板(即选项卡):“new”、“shared”、“dedicated”。
<task>.dependsOn
和 <task>.dependsOrder
指定它们的依赖关系和顺序。
有了上面的所有这些参数,你最终可能会在你的
tasks.json
中得到类似的东西:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Run the first portion",
"type": "shell",
"command": "sleep", // Your program goes here
"args": ["2"], // Your arguments goes here
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "new",
"showReuseMessage": true,
"clear": false
},
},
{
"label": "Run the second portion",
"type": "shell",
"command": "echo",
"args": ["Right after the first portion"],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "new",
"showReuseMessage": true,
"clear": false
},
},
{
"label": "Run all portions",
"type": "shell",
"dependsOrder": "sequence",
"dependsOn": [
"Run the first portion",
"Run the second portion",
],
}
]
}
它为您提供 3 项任务。
Run all portions
是主要的。但你仍然可以单独执行其他人。
注意:任务将在不同的面板/选项卡中按顺序执行,但很容易控制。如果预计第一部分不会一直在新面板中停止或执行,您只需要稍微调整一下即可。
注 2:如果您需要交互式调试器功能,请查看
launch.json
。
runas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas .exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas .exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exerunas.exe