QuaDECH
QuaDECH

Reputation: 35

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

Ok, so I am going to write a script and I need it to auto open xterm when I double-click it (similar to a BAT on Windows). It needs to work on ANY desktop that includes xterm. So for example it would do a check if it was already running in xterm then if not it would reopen itself in xterm and if it is the script will continue its process. Sorry if this has already been asked but I was unable to find anything for the life of me.

Upvotes: 1

Views: 831

Answers (2)

Thomas Dickey
Thomas Dickey

Reputation: 54475

Bearing in mind that it is possible for other scripts to set environment variables (including those done for the wrong reasons), you can check one of the environment variables set by xterm. For instance (see 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).

That variable was added in 2005 (patch #202), so it should be available on any system to which you have access.

In a script, you can do this check in one line, e.g.,

#!/bin/sh                                                           
test -z "$XTERM_VERSION" && exec xterm -e $0 $*
view $*

This checks if the given variable is set, and if not, it transfers control to xterm passing the information needed to run the script from the start.

Upvotes: 0

EternalHour
EternalHour

Reputation: 8621

You can do this as follows:

xterm -e program [ arguments ... ]

From the linux man page...

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.

Upvotes: 1

Related Questions