double-clicking on a shell script

All Gurus once were Newbies
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Please stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions prefer the other forums within the support section.
Before you post please read how to get help
Post Reply
Yiannis
Level 1
Level 1
Posts: 20
Joined: Sat Oct 13, 2012 5:30 am

double-clicking on a shell script

Post by Yiannis » Sat Oct 13, 2012 5:39 am

Ok, I am a bit baffled with one.

I just installed Mint as a dual boot on my W7 system. This was after trying Ubuntu (through Wubi) for some months as my first attempt to Linux world.
While on Ubuntu I created a couple of very simple scripts and saved them in the desktop. By double-clicking them the scripts were running.

Now, I transferred these scripts to the Mint's desktop and whenever I double click them nothing happens. Right clicking on them to see the options I have, I see nothing that can help (e.g. "run in terminal"). I have all the permissions for these scripts.

Any idea what I am doing wrong? Or can you point me to a thread/website?

Thank you in advance.

P.S. Strangely, a google search was not much of a help nor a search in the forum.

User avatar
Illphrin
Level 1
Level 1
Posts: 11
Joined: Tue Aug 21, 2012 6:10 pm

Re: double-clicking on a shell script

Post by Illphrin » Sat Oct 13, 2012 6:13 am

Hello Yiannis,

If you run it on a terminal what happen?

Yiannis
Level 1
Level 1
Posts: 20
Joined: Sat Oct 13, 2012 5:30 am

Re: double-clicking on a shell script

Post by Yiannis » Sat Oct 13, 2012 6:15 am

It runs absolutely fine.

Btw.

Code: Select all

Linux yiannis-Mint 3.2.0-23-generic #36-Ubuntu SMP Tue Apr 10 20:39:51 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

User avatar
Illphrin
Level 1
Level 1
Posts: 11
Joined: Tue Aug 21, 2012 6:10 pm

Re: double-clicking on a shell script

Post by Illphrin » Sat Oct 13, 2012 6:37 am

Hum...
If you do a right-click on your file script > Properties and then you coul see a box "Allow to run as a program" to tick.
Now it should work if you do a double-click on your script.

Yiannis
Level 1
Level 1
Posts: 20
Joined: Sat Oct 13, 2012 5:30 am

Re: double-clicking on a shell script

Post by Yiannis » Sat Oct 13, 2012 6:48 am

There is not such a box to tick but I guess you mean the "is executable" box which is already ticked.

This is a screenshot:
https://skydrive.live.com/redir?resid=A ... D1B91!9322

And this is a test script

Code: Select all

#!/bin/bash
echo "test_script"

$SHELL

User avatar
Illphrin
Level 1
Level 1
Posts: 11
Joined: Tue Aug 21, 2012 6:10 pm

Re: double-clicking on a shell script

Post by Illphrin » Sat Oct 13, 2012 7:45 am

Hum...
I don't know what you could do. Try changing randomly the settings maybe :D

Yiannis
Level 1
Level 1
Posts: 20
Joined: Sat Oct 13, 2012 5:30 am

Re: double-clicking on a shell script

Post by Yiannis » Mon Oct 22, 2012 7:49 pm

Anybody guys??

It started becoming annoying now that I realised that not even python scripts run when I double click on them.

The installation is still fresh as I haven't installed anything in particular apart from the alacarte.

Any help/advice will much appreciated.

User avatar
xenopeek
Level 24
Level 24
Posts: 22926
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: double-clicking on a shell script

Post by xenopeek » Tue Oct 23, 2012 6:46 am

Well, the bash example script you gave I think will exit within a millisecond and cause the terminal to close again? I'm not that familiar with KDE, but you could check Dolphin's (file manager) preferences on the default behavior for scripts. At least on the Nautilus file manager you can configure what options you want to be given when starting a script.
Image

Yiannis
Level 1
Level 1
Posts: 20
Joined: Sat Oct 13, 2012 5:30 am

Re: double-clicking on a shell script

Post by Yiannis » Tue Oct 23, 2012 6:56 am

Thanks for the reply.

No, it will not exit. The $SHELL at the end ensures exactly that, that it will return to the terminal (or at least it did so in ubuntu 12.04).
Last night I figured out that the shell does run but in the "background". For instance the script:

Code: Select all

#!/bin/bash
echo "test_script" >>testfile
$SHELL
creates a text file with the name testfile in the same directory with the string "test_script" in it. In Dolphin's setting I did not find anything relevant about sunning scripts.
Also, by modifying the script such as:

Code: Select all

#!/bin/bash
Konsole -e echo "test_script" &
$SHELL
did not solve the problem.

User avatar
xenopeek
Level 24
Level 24
Posts: 22926
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: double-clicking on a shell script

Post by xenopeek » Tue Oct 23, 2012 8:01 am

I always include the following header in my BASH scripts, so when they aren't run from a terminal they will relaunch themselves from a terminal and wait for you to close the terminal before exiting.

Code: Select all

#!/bin/bash

# if the script was not launched from a terminal, restart it from a terminal
if [[ ! -t 0 ]] && [[ -x /usr/bin/x-terminal-emulator ]]; then
   /usr/bin/x-terminal-emulator -e "bash -c \"$0 $*; read -s -p 'Press enter to continue...'\""
   exit
fi
Image

Yiannis
Level 1
Level 1
Posts: 20
Joined: Sat Oct 13, 2012 5:30 am

Re: double-clicking on a shell script

Post by Yiannis » Tue Oct 23, 2012 9:16 am

Interesting, but it just returns an "empty" terminal.

User avatar
xenopeek
Level 24
Level 24
Posts: 22926
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: double-clicking on a shell script

Post by xenopeek » Tue Oct 23, 2012 10:18 am

So the following doesn't show a terminal with the text "test_script" on it? Which terminal are you using, because the default KDE terminal will show "test_script" when run with the below.

Code: Select all

#!/bin/bash

# if the script was not launched from a terminal, restart it from a terminal
if [[ ! -t 0 ]] && [[ -x /usr/bin/x-terminal-emulator ]]; then
   /usr/bin/x-terminal-emulator -e "bash -c \"$0 $*; read -s -p 'Press enter to continue...'\""
   exit
fi

echo "test_script"
Image

Yiannis
Level 1
Level 1
Posts: 20
Joined: Sat Oct 13, 2012 5:30 am

Re: double-clicking on a shell script

Post by Yiannis » Tue Oct 23, 2012 10:42 am

I am using the Konsole (if this is what you are asking - sorry, complete newbie here).

When I run it - and after a delay of about 500ms - it just returns: yiannis@yiannis-Mint ~ $ and that's all.

Again many thanks for taking the time to respond.

User avatar
elfaure
Level 1
Level 1
Posts: 41
Joined: Tue Jun 18, 2013 10:18 pm

Re: double-clicking on a shell script

Post by elfaure » Sat Jul 13, 2013 8:04 pm

LM-KDE (Linux Mint-KDE):

[Edit] I found a better way...either way, you have to make a launcher for your shell script. Just create a launcher, browse and select your script as the app, edit the command line to add an "sh" or "bash" in front of its path, then go into advanced settings and tell it to be run from the terminal. If any of your commands are sudo, it will pop up a terminal and prompt for you for your sudo password (once) then execute the script. Its a blind execution, and even if you tell the terminal to stay open after the script runs, there is no stdout nor stderr. You will have to find another way to check to see that your script ran successfully (check for the results of your script, not its terminal output). If your script runs manually from the terminal, it will run in the launcher the same way.

Right-click an open area or on most icons in any *panel* [bottom bar] (try a few different spots, sometimes it doesn't stick for open areas) and look for "+ Add Launcher".

####################################################################################################################

Depreciated for the above solution:

If on KDE, that's a whole different story...you have to edit the application launcher menu (right-click, edit applications, new item...). I just figured this out and got it working on KDE. You create your own launcher on the menu.

LMDE (Linux Mint Desktop Edition):

Not sure how to directly double-click the script, but in Mint its simple. Create a launcher (link to an executable) with the following command:

Code: Select all

sh /path_to_your_script/your_script_name.sh
sh should already be in your path "echo $PATH". If not (for some crazy reason, replace sh with /bin/sh above). Your script can either have or not have a file extension (user preference). Make sure under properties for your script, it is marked as executable. Choose an icon if you don't want to look at a spring-board. Done.

If you want to use the Bourne Again Shell (bash) rather than the Bourne Shell (sh), replace sh with bash above except for the file extension.

Post Reply

Return to “Newbie Questions”