16

Say I need to write a script that will launch a terminal and execute a command and I need that to work on various systems. How can I do that in a safe and portable manner?

Is xterm always installed? I assume that any *nix with a GUI must have some terminal emulator. How can I get to it? Debian systems have x-terminal-emulator but that, apparently, does not even work in Red Hat, let alone non-Linux *nixes.

terdon
  • 242,166
  • This might be doable with expect – Red Cricket Sep 20 '13 at 16:28
  • @RedCricket expect? How? By asking the user to type in their terminal? – terdon Sep 20 '13 at 16:32
  • .. Curious, why would you need to do this? Perhaps we can suggest an alternate solution. – user606723 Sep 20 '13 at 18:56
  • @user606723 basic curiosity, wondering if there is such a thing as a POSIX or opendesktop defined default terminal emulator (looks like the answer is no). Came as a reaction to this SU question. – terdon Sep 20 '13 at 18:57
  • do you really need to launch a terminal emulator, or do you actually just need to get a shell. That's quite a different need there. – Lie Ryan Sep 20 '13 at 20:14
  • @terdon, ah. Yeah, I don't think there is any legit reason to do this that wouldn't be in the realm of person scripting. Therefore, there hasn't been any need for such a standard. – user606723 Sep 20 '13 at 20:52

2 Answers2

11

I don't think you can. xterm need not be installed everywhere, and indeed probably isn't by default. Especially when a desktop environment is in use that provides its own terminal.

I think your best bet is probably to check for the existence of a few different terminals (say, xdg-terminal, x-terminal-emulator, gnome-terminal, konsole, xterm). And maybe work towards getting xdg-terminal actually added to the FreeDesktop.org standards.

Then you'll get to find that different terminals have different ways to run commands, and sometimes even different versions of the same terminal... e.g., see Debian Bug #648271.

You should probably also give the admin/user a way to set a custom command. It'll surely be needed.

derobert
  • 109,670
  • Yeah, this was prompted by a question on SU and I was hoping there would be a simpler way than a bunch of case statements. – terdon Sep 20 '13 at 16:23
  • 1
    @terdon I guess the simple way would be to list a fairly dependency-light terminal as a requirement for the script, then just use it. – derobert Sep 20 '13 at 16:26
  • How about that terminal window that used to appear when I ran xinit? Haven't done so in years but that used to load a simple X server with an empty xterm(?) window from which you could start your session. Doesn't that imply that some terminal emulator is installed with X? – terdon Sep 20 '13 at 17:17
  • @terdon it used to be, but then the xorg people split up X into a bunch of packages. (That terminal was probably xterm). But a quick check shows I could remove xterm from my system if I wanted to. – derobert Sep 20 '13 at 17:23
  • @terdon That's xterm, the traditional “default” X terminal emulator. But it may not always be installed. IIRC the default Ubuntu installation includes only gnome-terminal, not xterm (and for Kubuntu it would be konsole, etc). – Gilles 'SO- stop being evil' Sep 20 '13 at 22:44
3

Yeah you need to use a wrapper. There is a good one included with i3 WM. Explained here: http://build.i3wm.org/docs/i3-sensible-terminal.html

Just copy this into a shell function in your script: http://code.stapelberg.de/git/i3/tree/i3-sensible-terminal