使用循环中的复杂命令在 gnome 终端中打开多个选项卡
我有一个需要这样调用的命令:
command "complex argument"
如果我想运行 gnome-terminal 并传递这个参数,它会像这样:
gnome-terminal -e 'command "complex argument"'
我想在终端中打开多个选项卡,每次使用不同的参数执行此命令。这是这样工作的:
gnome-terminal -e 'command "complex argument1"' --tab -e 'command "complex argument2"'
但是如果我想用脚本执行它,问题就来了,在脚本中我从一个循环中获取每个选项卡的参数(即选项卡的数量是可变的)。我的基本想法是收集单个变量的参数,然后将其传递到 gnome-terminal。但我不知道如何做到这一点,使所有嵌套的引用参数保持不变。要么所有内容都压缩在一个参数中(如果我调用 gnome-terminal "$args"),要么它被每个空格分开(如果我调用 gnome-terminal $args )。
有没有办法在 bash 中组成如此复杂的参数?或者,有什么方法可以将 IPC 消息发送到 gnome-terminal,告诉它打开一个新选项卡并执行命令?我知道我可以使用 Konsole 来完成此操作,但现在我想使用 gnome-terminal 来完成此操作。
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 -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
).
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我刚刚遇到了同样的问题,并在尝试解决它时遇到了这篇文章。如果“复杂参数”依赖于 shell 扩展,我相信您将需要使用传递到 gnome-terminal 的命令来启动 shell。例如:
您可以启动 bash 或任何其他 shell,而不是 sh。有关更多示例,请参阅此堆栈交换帖子。
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:
You can start bash or any other shell instead of sh. For more examples see this stack exhange post.
不确定这是否会有帮助,因为这篇文章已经发布了大约一年,但我在 BASH 中编写 gnome-terminal 脚本时遇到了类似的问题。我的答案和riachdesign类似,但是转义字符不同。这就是我所做的:
如果我没有在 $dir 周围添加单引号(例如,$dir 与 '$dir'),则该行将逐字执行(即,它不会将变量的内容传递到该行中)。
希望这有帮助。
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:
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).
Hopefully that helps.
AFAIK,您可能需要转义双引号(或单引号,无论您在 $args 周围使用什么),就像这样。 ('命令\"复杂参数1\"' --tab -e '命令\"复杂参数2\"')
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\"')
看看这个 ruby gem,它确实做到了这一点:https://github.com/Achillefs/elscripto
Have a look at this ruby gem that does exactly that: https://github.com/Achillefs/elscripto
我找到了解决方案:数组。他们可以施展魔法。
I found the solution: arrays. They can do magic.