double-clicking on a shell script

Quick to answer questions about finding your way around Linux Mint as a new user.
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. Stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions use the other forums in the support section.
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
Yiannis

double-clicking on a shell script

Post by Yiannis »

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.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Illphrin

Re: double-clicking on a shell script

Post by Illphrin »

Hello Yiannis,

If you run it on a terminal what happen?
Yiannis

Re: double-clicking on a shell script

Post by Yiannis »

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
Illphrin

Re: double-clicking on a shell script

Post by Illphrin »

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

Re: double-clicking on a shell script

Post by Yiannis »

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
Illphrin

Re: double-clicking on a shell script

Post by Illphrin »

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

Re: double-clicking on a shell script

Post by Yiannis »

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 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: double-clicking on a shell script

Post by xenopeek »

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

Re: double-clicking on a shell script

Post by Yiannis »

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 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: double-clicking on a shell script

Post by xenopeek »

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

Re: double-clicking on a shell script

Post by Yiannis »

Interesting, but it just returns an "empty" terminal.
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: double-clicking on a shell script

Post by xenopeek »

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

Re: double-clicking on a shell script

Post by Yiannis »

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.
elfaure

Re: double-clicking on a shell script

Post by elfaure »

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.
Locked

Return to “Beginner Questions”