Open Terminal, Execute a Command, and Keep Terminal Open
- November 12th, 2010
- Posted in CrunchBang Linux . Debian . Linux . NCLUG . Open Source / FOSS . OpenBox Window Manager . PekWM Window Manager . Ubuntu, Kubuntu, Xubuntu, Fluxbuntu, Nubuntu, Geubuntu . Useful Software
- Write comment
A while back, someone asked me if I knew of a way to launch a terminal session from the Gnome panel, execute a command, and keep the terminal open. The example that I will use is launching the nmap help feature. Surely there are multiple ways to do this. Of course you could always put the ‘&’ character after your command, but what if you wish to launch a command followed by a flag or by its own argument, such as with “nmap –help“? Try executing the following command from your panel or menu and see if it works; it does NOT work for me.
xterm -e nmap --help &
I’ve created a very simple script called helperScript.sh. Download it and save it to your home directory, or some other location of your choosing; rename it so that you loose the “.txt” file extension and then make the file executable (chmod 755 helperScript.sh). In this example, I will assume that you have saved the file to your home directory. Like I said… the script is pretty simple:
#! /bin/bash $@ /bin/bash
This script should be called from your panel/menu launcher using a command structure that will launch your terminal emulator, then call on this shell script, and then provide an argument that is taken in by this script (“$1” or $@); this argument tells the script which command to run (for instance: nmap –help).
SYNTAX: [command] [path/to/helperScript.sh] [argument]
When launching the script from your panel or menu, please note that:
– The word “USER” should be changed to your own username
– Change “nmap –help” to whatever is specific to your needs
– Make sure that helperScript.sh is executable (chmod 755 helperScript.sh)
The command you should use in your panel/menu launcher is as follows.
Launch with gnome-terminal:
gnome-terminal -x /home/USER/helperScript.sh nmap --help
Launch with xterm:
xterm -e /home/USER/helperScript.sh nmap --help
Launch with Terminator:
terminator -x /home/USER/helperScript.sh nmap --help
Note: [-x] and [-e] are the flags for “execute”, depending on which terminal you are using. For some reason, “terminator -x” will launch some things properly using Terminator, but not others; I’m not sure why at this time.
At the bottom of the script, the reference to /bin/bash tells the terminal to keep a bash shell open and running.
Credit is due to 13u11fr09 for coming up with the basic concept for this script; read his/her thread at http://ubuntuforums.org/archive/index.php/t-296628.html.