bash 使用循环中的复杂命令在 gnome 终端中打开多个选项卡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6611542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:19:23  来源:igfitidea点击:

Opening multiple tabs in gnome terminal with complex commands from a cycle

bashscriptingtabsgnome-terminal

提问by petersohn

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

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

command "complex argument"

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

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

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"'

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).

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

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.

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

采纳答案by petersohn

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[@]}"

回答by Matt Liberty

I just ran into this same problem and came across this post while trying to fix it. If "complex argument" relies upon shell expansion, I believe that you will need to start a shell with the command passed to gnome-terminal. For example:

我刚刚遇到了同样的问题,并在尝试修复它时遇到了这篇文章。如果“复杂参数”依赖于外壳扩展,我相信您将需要使用传递给 gnome-terminal 的命令来启动外壳。例如:

gnome-terminal \
--tab -e "sh -c 'command \"complex argument1\"'" \
--tab -e "sh -c 'command \"complex argument2\"'"

You can start bash or any other shell instead of sh. For more examples see this stack exhange post.

您可以启动 bash 或任何其他 shell 而不是 sh。有关更多示例,请参阅此堆栈交换帖子

回答by Prophet60091

Not sure this will help out as the post is about a year old, but I had a similar issue scripting gnome-terminal in BASH. My answer is similar to riachdesign, but the escape characters differ. Here's what I did:

不确定这是否会有所帮助,因为该帖子已有大约一年的历史,但我在 BASH 中编写 gnome-terminal 脚本时遇到了类似的问题。我的回答与 riachdesign 类似,但转义字符不同。这是我所做的:

gnome-terminal -e bash -c "/home/someprogramtorun /home/user/'$dir'/filetopasstoprogram.txt"

If I didn't add the single quotes around $dir (e.g., $dir vs. '$dir') the line would execute verbatim (i.e., it would not pass the variable's contents into the line).

如果我没有在 $dir 周围添加单引号(例如,$dir 与 '$dir'),该行将逐字执行(即,它不会将变量的内容传递到该行中)。

Hopefully that helps.

希望这有帮助。

回答by Achilles

Have a look at this ruby gem that does exactly that: https://github.com/Achillefs/elscripto

看看这个 ruby​​ gem 就是这样做的:https: //github.com/Achillefs/elscripto

回答by riachdesign

AFAIK, you may need to escape the double quotes (or single quotes, whatever you use around $args), like so. ('command \"complex argument1\"' --tab -e 'command \"complex argument2\"')

AFAIK,您可能需要像这样转义双引号(或单引号,无论您在 $args 周围使用什么)。('command \"complex argument1\"' --tab -e 'command \"complex argument2\"')