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.
Whoa, thanks a lot, seriously.
I couldn’t figure out how to do this and I was having problems “asking” Google the right thing.
Was I looking for this…
terminator -x open-term-exec-keep-open echo vim newfile &
Noob here, why does it work?
As I understand it, $@ gets resolved to a list of all command line arguments separated by spaces, but how does that go into the new terminal, and why is a new terminal even opened?
@Ciro Santilli, that’s a good question. I’m no self-proclaimed Bash expert, but my understanding is that if you were to use $1, that would allow you to take in one argument. If you were to use $1 $2 $3 $4, that would allow you to take in up to 4 arguments. My understanding is that in a shell script, $* or $@ refers to all arguments passed into the script.
Thank you for this! Exactly what I needed.
Great! BUT i am using LXDE (Lubuntu) — everything works but every time i click on my file i get this annoying message: “The file ‘weather’ is executable. Do you want to execute it?” Of course i want to execute it!!! It gives me three choices, “Execute,” Execute in Terminal,” and “Cancel.” When i click “Execute” it runs just fine, but how do i get it so i click on it and it just executes without double-checking with me first??
FWIW it’s just a little command to get my forecast:
lxterminal -e /home/dad/helperScript.sh weather-util –id=KELM –forecast –city=Elmira –st=NY &
Never mind, i answered my own question. As soon as i added it as a menu entry the problem disappeared. I suppose it has to do with me trying to start it from a file manager…thanks for the script!!!