2

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.

4
  • 1
    I could make a script that opens terminal and runs the file you selected. It would be accessible from right click menu though. Not sure if possible to apply the double click to scripts , i will look into it , but the nautilus script i propose would be 100% a working solution – Sergiy Kolodyazhnyy Aug 29 '16 at 23:51
  • If it's just: 1.Right click. 2.Click "Run in Terminal" (possibly two scripts instead of one, one that runs the script with sudo, one without) 3.Terminal window pops up) then it would be cool. – Manchineel Aug 30 '16 at 7:31
  • I apologize for the delay in answering this question, but sometimes life gets in the way of coding :) Answer posted, let me know how you like it. – Sergiy Kolodyazhnyy Sep 9 '16 at 11:02
  • No, don't worry. Thanks very much – Manchineel Sep 9 '16 at 11:17
3

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.

4
  • Thanks. How do I add this to RightClick menu? – Manchineel Sep 9 '16 at 11:21
  • 1
    @alex2003super simply place the script into ~/.local/share/nautilus/scripts folder. Just make sure it has executable permissions, otherwise it won't show up. – Sergiy Kolodyazhnyy Sep 9 '16 at 11:24
  • Thanks. So, just place it there, right click, go into properties, permissions, "Allow execution of file as a program", right? – Manchineel Sep 9 '16 at 11:26
  • @alex2003super yes, exactly right – Sergiy Kolodyazhnyy Sep 9 '16 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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