使用循环中的复杂命令在 gnome 终端中打开多个选项卡 [英] Opening multiple tabs in gnome terminal with complex commands from a cycle

查看:28
本文介绍了使用循环中的复杂命令在 gnome 终端中打开多个选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个命令需要这样调用:

I have a command that needs to be called like this:

command "complex argument"

如果我想运行 gnome-terminal 并传递这个参数,它是这样的:

If I want to run gnome-terminal passing it this argument, it goes like this:

gnome-terminal -e 'command "complex argument"'

我想在终端中打开多个选项卡,每次使用不同的参数执行此命令.这是这样工作的:

I want to open multiple tabs in the terminal, executing this command with different arguments each time. This works this way:

gnome-terminal -e 'command "complex argument1"' --tab -e 'command "complex argument2"'

但是如果我想用脚本执行它,问题就来了,我从一个循环中获取每个选项卡的参数(即选项卡的数量是可变的).我的基本想法是我将参数收集到单个变量,然后将其传递给 gnome-terminal.但我不知道如何做到这一点,使所有嵌套的引用参数保持不变.要么所有东西都压缩在一个参数中(如果我调用 gnome-terminal "$args"),或者它被每个空格分开(如果我调用 gnome-terminal $args).

But the problem comes if I want to execute it with a script, where I get the parameters for each tabs from a cycle (i.e. the number of tabs is variable). My basic idea was that I collect the arguments to a single variable, then pass it to gnome-terminal. But I don't know how to do this leaving all the nested quoted arguments intact. Either everything is compressed in one argument (if I call gnome-terminal "$args"), or it falls apart by every whitespace (if I call gnome-terminal $args).

有什么方法可以在 bash 中组合如此复杂的参数?或者,有没有办法将 IPC 消息发送到 gnome-terminal,告诉它打开一个新选项卡并执行命令?我知道我可以用 Konsole 做到这一点,但现在我想用 gnome-terminal 做到这一点.

Is there any way to compose such complex arguments in bash? Or, alternatively, is there any way to send IPC messages to gnome-terminal, telling it to open a new tab and execute a command? I know I can do this with Konsole, but now I want to do it with gnome-terminal.

推荐答案

我找到了解决方案:数组.他们会变魔术.

I found the solution: arrays. They can do magic.

# initial arguments
command=(gnome-terminal -e 'command "complex argument"')
...
# add extra arguments
command=("${command[@]}" --tab -e 'command "complex argument2"')
...
# execute command
"${command[@]}"

这篇关于使用循环中的复杂命令在 gnome 终端中打开多个选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆