LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How can I run a .sh file by double-clicking on it??? (http://www.linuxquestions.org/questions/linux-newbie-8/how-can-i-run-a-sh-file-by-double-clicking-on-it-813410/)

kevinx17910 06-10-2010 03:02 PM

How can I run a .sh file by double-clicking on it???
 
Hi everyone,

I am new to linux and bash programming. I wrote a simple hello world program.
the code:

#!/bin/bash
echo hello world

but I can not run it by clicking on it.
The only way I ran it is using the terminal even after I check the is it executable box.

Do I need to add any commands to make it execute by just clicking on it???

Thx in advance

GrapefruiTgirl 06-10-2010 03:12 PM

While it's difficult to detect, some systems configurations allow scripts to be executed by clicking or double-clicking, and yours may actually be running just fine; but the trouble is, since they are designed to be run by the shell, in a terminal (where the output would end up) or by the shell in non-interactive mode, you are not seeing any evidence that it is running. Scripts are not intended to really be 'double-clicked' -- they are supposed to be run from a terminal window.

You might be able to configure your system settings to change the "Run with..." property, and change that property to xterm or gnome-terminal or whatever terminal you want to use.

As a test, for the benefit of both of us, try this:

Code:

#!/bin/bash

echo "hello world" >> testfile

save the above file as whatever you like, set it to 'executable', and then double-click it. If the thing runs, you will end up with a new text file named "testfile" in the same directory you are in, and it will contain the "hello world" entry. If this works, it proves that the script(s) do in fact run when clicked, but just that you cannot see them run.

Hope this helps a little bit! Good luck with your scripting. Fun stuff!!

Sasha

kevinx17910 06-10-2010 04:33 PM

First of all thx for replying.

I updated the file and ran it. It indeed created the testfile. But is there any way to make the hello world text show up in a terminal?? just like the windows batch file???

jefro 06-10-2010 09:44 PM

Go with GrapefruiTgirl's ideas.

Just an idea maybe.

Might be simple to to a script to run xterm and then might use expect or other way to input code.

I am sure I have this wrong but someone will correct it hopefully.

#!/bin/bash
xterm& echo hello world


I know I have that wrong.

http://www.linuxquestions.org/questi...script-561451/

orangesky 06-10-2010 10:22 PM

Typically, .sh scripts are installers and have to be run as root anyway.

btmiller 06-10-2010 11:19 PM

Quote:

Originally Posted by orangesky (Post 3999717)
Typically, .sh scripts are installers and have to be run as root anyway.

Not really -- shell scripts have a variety of uses beyond installers and (depending on their purpose) may not need to be run as root. At work I have a number of shell scripts that I use to automate various tasks.

John VV 06-10-2010 11:43 PM

as with btmiller i have a BUNCH of "handy" scripts i use for my every day work

now it depends on what it dose
If it opens a gui then the setting in nautilus will work ( there is a gui setting for this ) in gnome . normally when you click on it a dialog option opens up and asks what you want to do .


BUT if it is a text only output or a non gui program ( it outputs an image/photo ) you will NOT see output unless you start it from bash

Kenhelm 06-11-2010 02:22 AM

This method works on my system.
First the script tests to see if it is running in a terminal.
If it isn't then it runs itself again but this time in a terminal.
Code:

#!/bin/bash
if ! tty -s; then xterm -e "$0"; exit; fi
sleep 1
echo "hello world"
echo "Press a key to exit"
read -sn1


jefro 06-11-2010 03:59 PM

That's what I am talking about.

kevinx17910 06-11-2010 04:27 PM

thank you guys for the solutions they are very helpful.
Here is another problem:

I am using backtrack so the terminal is called konsole
if I use the command konsole -e, ex. konsole -e echo hello world.
It means to open another konsole and run the command echo hello world in the new konsole. the problem is in order to run the rest of the command in the script, i have to close the new hello world konsole.
Is there any way to run the rest of the script without closing the new konsole window????
Thx in advance

GrapefruiTgirl 06-11-2010 04:42 PM

Probably forking off the subprocess from the main process should work. To do this, add an ampersand like so:

Code:

konsole -e echo hello world &
That should cause your "hello world" console to start and do its thing, while returning control back to the originating script.

Note that there are a LOT of different term emulators (Consoles) for Linux, and not all of them behave the same when spawning them from a script; you may have to fiddle around a bit.

Sasha

jefro 06-11-2010 05:45 PM

Even better.


All times are GMT -5. The time now is 03:02 AM.