Today, we're going to look at how you launch a shell script (or other terminal-based application) in GNOME.  We are all familiar with launching applications from the command line.  There are advantages to this even for for graphical applications.  When you launch an application on the command line, you can see what is produced on standard output and standard error, which can sometimes provide valuable diagnostic information.  On the other hand, there is something to be said for double-click convenience as well.

We will write a tiny shell script and then set up GNOME to allow launching the script from a desktop icon.  Here's the script we will use:

#!/bin/bash

# touch_foo - Script to touch the file "foo"

if [ ! -d foo ]; then
    touch foo
    echo "foo is touched."
else
    echo "foo is a directory."
fi

Pretty simple.  This script touches a file name "foo" but if a directory with that name already exists, it displays a message and exits.  We will enter this script into our text editor and save it as ~/bin/touch_foo.

Next we need to create a launcher for the script.  To do this, we right click on the GNOME desktop and select "Create Launcher...":


A dialog box will appear where we can enter information about the launcher:


Since we want to see the script execute, we will select "Application in Terminal," otherwise the script will execute silently in the background:


Next, we give the launcher a name, browse for the script file, and fill in an optional comment:


You may also click on the icon at the right of the dialog box and select another icon for the launcher.  After the dialog box is filled out, we can click the OK button and the launcher is created and it will appear on the desktop.  Double clicking on the new launcher icon launches the script.

But there's a problem.  The terminal appears for an instant and vanish.  Not exactly what we had in mind.  What gives?

The problem is that when the script finishes, the terminal session ends.  To prevent this, we have to stop the script from finishing.  We could do this by having an endless loop at the end of the script, or by including a read command which will wait for user input before proceeding.  We can adjust our script by adding a line at the end:

#!/bin/bash

# touch_foo - Script to touch the file "foo"

if [ ! -d foo ]; then
    touch foo
    echo "foo is touched."
else
    echo "foo is a directory."
fi

read -p "Press Enter to continue > "

After making this change, a terminal will appear and wait until the Enter key is pressed:


Another Approach

Another way we can configure commands for the launcher is to directly launch gnome-terminal and pass the desired command as an argument.  This allows us to control the configuration of the terminal.  For example we can set the window title, window geometry, and terminal profile used with our command.  Here is a command we can use to launch the top program and set the window title to "Top":

gnome-terminal -e /usr/bin/top -t Top

Note however, that if you want to run shell scripts this way, you must construct the command this way:

gnome-terminal -e 'bash -c touch_foo'

including the bash program in the command so that there is a program present that can interpret your script.

Further Reading



--
Posted By William Shotts to LinuxCommand.org: Tips, News And Rants at 2/19/2010 04:34:00 PM