当前位置: 首页>>技术教程>>正文


在另一个(新)终端窗口上运行命令

, ,

问题描述

如何在另一个终端窗口中运行任何命令?

示例:我打开了一个终端窗口,如果我运行像 apropos editor 这样的命令,那么它会在该窗口上运行和 out-puts。但我想在另一个终端窗口(新窗口)上运行相同的命令,而不是在第一个终端的当前窗口上运行。

进一步说明:我需要 suggest-command <command> 来打开新的终端窗口并在该(新打开的)窗口中运行提到的 <command>。 (其中 suggest-command 是命令建议的示例。)

怎么做?

最佳方法

这可能是您搜索的内容:

gnome-terminal -e "bash -c \"!!; exec bash\""

或(不久):

gnome-terminal -x sh -c "!!; bash"

它打开 gnome-terminal 并执行您的最后一个命令 ( !! ) 并且它在 shell 中的命令输出中保持打开状态,即使是像 topless 这样的交互式命令…

在你的情况下:

gnome-terminal -e "bash -c \"apropos editor; exec bash\""

要么

gnome-terminal -x sh -c "apropos editor; bash"

次佳方法

启动您要运行的任何终端的另一个实例:

xterm -hold -e 'apropos editor' & 

注意 -hold 。大多数终端将在运行您提供给它们的命令后退出。网站上已经有十几个关于这个的问题:

另一种方法是使用需要退出的应用程序。 nano 将自行保持打开状态。如果您只是输出到屏幕,则可以将其通过管道传输到 less

xterm -e 'apropos editor | less' & 

也就是说,在您的情况下(正如其他两个所说),您只需打开另一个终端并运行您的命令似乎更容易。

第三种方法

每个终端甚至是一个程序,您可以像任何其他程序一样启动它,将 & 置于后台,提供参数列表等等。

使用哪个终端首先取决于您使用的系统的可用性(如果它们已安装或未安装),其次取决于它们的特性,然后取决于您的个人品味。

  konsole   --hold -e "ls" &  
  xterm      -hold -e "ls" &  
  gnome-terminal   -e "ls" & ...  

请注意 xterm-holdkonsole--hold 之间的差异。

每个实现都有不同的选项,您必须通过帮助进行检查。甚至可以以不同的方式调用帮助。您会发现 man konsole 不起作用,因此您必须使用 --help 直接询问可执行文件。

这是您可以在系统上搜索的终​​端列表

aterm          - AfterStep terminal with transparency support
gnome-terminal - default terminal for GNOME
guake          - A dropdown terminal for GNOME
konsole        - default terminal for KDE
Kuake          - a dropdown terminal for KDE
mrxvt          - Multi-tabbed rxvt clone
rxvt           - for the X Window System (and, in the form of a Cygwin port, 
                 for Windows) 
rxvt-unicode   - rxvt clone with unicode support
xfce4-terminal - default terminal for Xfce desktop 
                 environment with dropdown support
Terminator     - is a GPL terminal emulator. It is available on
                 Microsoft Windows, Mac OS X, Linux and other Unix X11 systems.
Terminology    - enhanced terminal supportive of multimedia 
                 and text manipulation for X11 and Linux framebuffer
tilda          - A drop down terminal
wterm          - It is a fork of rxvt, designed to be lightweight, but still
                 full of features
xterm          - default terminal for the X Window System
Yakuake        - (Yet Another Kuake), a dropdown terminal for KDE

第四种方法

  1. 打开两个终端。

  2. 使用 tty 命令识别每个终端。

  3. 假设他们用 /dev/pts/0/dev/pts/1 标识。

  4. 在终端 pts/0 中,使用 exec 命令将标准输出重定向到 pts/1:exec 1>/dev/pts/1

  5. 现在 pts/0 终端的每个命令 stdout 输出都显示在 pts/1 中。

  6. 使用命令重定向标准输出:exec 1>/dev/pts/0

  7. 现在 pts/0 标准输出像以前一样工作。

第五种方法

您可以对 gnome-terminal 使用 -e 选项,如下所示:

gnome-terminal -e 'sh -c propose editor'

这里 sh 是 gnome-terminal 打开的 shell 。请注意,这将在命令终止后立即退出终端。更多信息请参考 the manual page for gnome-terminal

第六种方法

在 Ubuntu 18.04 LTS 之后,您可能希望从 -e 切换到 -- ,即 gnome-terminal -egnome-terminal -- 因为 -e-x 都已弃用。

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/12071.html,未经允许,请勿转载。