LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-10-2010, 03:02 PM   #1
kevinx17910
LQ Newbie
 
Registered: Jun 2010
Posts: 10

Rep: Reputation: 0
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
 
Old 06-10-2010, 03:12 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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
 
Old 06-10-2010, 04:33 PM   #3
kevinx17910
LQ Newbie
 
Registered: Jun 2010
Posts: 10

Original Poster
Rep: Reputation: 0
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???
 
Old 06-10-2010, 09:44 PM   #4
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,982

Rep: Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625
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/

Last edited by jefro; 06-10-2010 at 09:46 PM.
 
Old 06-10-2010, 10:22 PM   #5
orangesky
Member
 
Registered: Jun 2009
Posts: 87

Rep: Reputation: 17
Typically, .sh scripts are installers and have to be run as root anyway.
 
Old 06-10-2010, 11:19 PM   #6
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Quote:
Originally Posted by orangesky View Post
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.
 
Old 06-10-2010, 11:43 PM   #7
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,624

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
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
 
Old 06-11-2010, 02:22 AM   #8
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
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
 
Old 06-11-2010, 03:59 PM   #9
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,982

Rep: Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625
That's what I am talking about.
 
Old 06-11-2010, 04:27 PM   #10
kevinx17910
LQ Newbie
 
Registered: Jun 2010
Posts: 10

Original Poster
Rep: Reputation: 0
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
 
Old 06-11-2010, 04:42 PM   #11
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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
 
1 members found this post helpful.
Old 06-11-2010, 05:45 PM   #12
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,982

Rep: Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625Reputation: 3625
Even better.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Create an executable file in linux which opens by double-clicking it shivanand Linux - General 5 08-30-2005 12:10 AM
KDE and double clicking Ravo Linux - General 2 02-20-2005 11:41 AM
How to run a script by double clicking it tom221 Linux - Newbie 6 02-07-2005 03:04 PM
enable Double clicking??? xhispage Linux - Newbie 2 10-19-2004 03:22 AM
File Manager and Double-clicking Jongi Linux - Newbie 3 09-04-2003 10:28 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration