Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
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
show 1 more comment

3 Answers

up vote 7 down vote accepted

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.

share|improve this answer
    
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 Sep 20 '13 at 22:44
add comment

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

share|improve this answer
    
That's a good start (+1) but only checks a limited number of terminals (and uses which which is a bad idea) –  terdon Sep 20 '13 at 18:44
add comment

Using Chrome! You can add this app, "Secure Shell". I use it all the time. This way, all you need to do is log in with Chrome and you are good to go, anywhere.

share|improve this answer
1  
How is 'use a specific OS' and install 'some software' a portable solution? –  terdon Sep 20 '13 at 18:19
1  
I think you completely misunderstood the question. Try reading it more closely, and if there are words that you don't understand (such as terminal, it seems), look them up. –  Gilles Sep 20 '13 at 22:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.