Ubuntu – Let Nautilus open script as Terminal window

gnome-terminalnautilusscripts

On Mac OS X, when you double click on a .sh file, a Terminal window pops up running the script and allowing me to interact with it.

On Ubuntu, by double clicking a script, I immediately see its effects: the script is ran. Only that, when I launch it that way, no command line window opens. Sure, I can just run the script from Terminal, but it would be cool to just have a Scripts folder, where they are organized in subfolders, and double click scripts to start them as Terminal windows. Note that I am not willing to create a Launcher (.desktop file), but I'd like to apply this globally.

Best Answer

Introduction

The script below puts together several ideas of how gnome-terminal and bash shell operate, to create a right-click menuentry for running a selected program in graphical terminal. Of course, this is not double-click via mouse, but still a usable and quick solution. It can be even speedier via right-click and hitting S key to quickly jump into "Scripts" submenu. Additionally, this can work with several selected files and will open terminal window for each one of them.

Demo

Suppose we have a tester script (which merely prints "Hello World" and exits) in some directory opened with Nautilus. Lets assume this script already has executable permissions. Right click on the file , select Scripts -> run_with_terminal.py

enter image description here

Terminal window will appear, and appropriately give the output, and wait till user hits Enter to exit.

Script Source

Also available on GitHub

#!/usr/bin/env python
from os import path
from sys import argv
from subprocess import call

for item in argv[1:]:
    full_path = path.abspath('./' + item)
    call(['gnome-terminal','-e', 
          "bash -c '" +  full_path + ";read'"])

Privileged version

#!/usr/bin/env python
from os import path
from sys import argv
from subprocess import call

for item in argv[1:]:
    full_path = path.abspath('./' + item)
    call(['gnome-terminal','-e', 
          "pkexec bash -c '" +  full_path + ";read'"])

Principle of operation

Gnome Terminal ( which is default terminal on Ubuntu ) allows running a command with -e flag, but problem is that it waits for command to exit. If you have a script or executable that only prints something to the screen and exits immediately, you'll only see terminal window flash and disappear.

Essentially it allows only running one command. On the other hand, if we use bash -c 'command1;command2;command3' , this will be treated by gnome-terminal as one command, but would allow us in fact to run several as child processes of the bash shell. This is nothing new and has been shown long before.

Finally, to keep window from immediately exiting, we use read command that just reads stdin. Basic use for this is to allow user to close window by hitting Enter key ( sort of adapted trick from using getch() from C programming in an IDE)

The file manager ( aka Nautilus ) allows use of custom scripts (placed in `~/.local/nautilus/scripts) that operate upon selected file/folder. They're accessible from right clicking on file and selecting appropriate entry in Scripts submenu.

Thus putting all of this knowledge together, we get the working script you see above. Python was merely a language of choice for me, but this same idea could have been implemented in either perl,ruby,shell script, etc.

Related Question