Linux: 如何在脚本 运行 时自动打开终端

Linux: How to auto open terminal when a script is run

好的,所以我要写一个脚本,我需要它在我双击它时自动打开 xterm(类似于 Windows 上的 BAT)。它需要在任何包含 xterm 的桌面上工作。因此,例如它会检查它是否已经在 xterm 中 运行,如果不是,它会在 xterm 中重新打开自己,如果是,脚本将继续其过程。很抱歉,如果有人问过这个问题,但我一生都找不到任何东西。

您可以按如下方式进行:

xterm -e program [ arguments ... ]

来自 linux 手册页...

This option specifies the program (and its command line arguments) to be run in the xterm window. It also sets the window title and icon name to be the basename of the program being executed if neither -T nor -n are given on the command line. This must be the last option on the command line.

请记住,其他脚本可能会设置环境变量(包括因错误原因而设置的环境变量),您可以检查xterm 设置的环境变量之一。例如(参见 manual):

XTERM_VERSION is set to the string displayed by the -version option. That is normally an identifier for the X Window libraries used to build xterm, followed by xterm's patch number in parenthesis. The patch number is also part of the response to a Secondary Device Attributes (DA) control sequence (see Xterm Control Sequences).

该变量是在 2005 年添加的 (patch #202),因此它应该可以在您有权访问的任何系统上使用。

在脚本中,您可以在一行中执行此检查,例如,

#!/bin/sh                                                           
test -z "$XTERM_VERSION" && exec xterm -e [=10=] $*
view $*

这会检查给定的变量是否已设置,如果没有,它会将控制权转移到 xterm,将所需的信息从一开始就传递给 运行 脚本。