LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 04: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, 04:12 PM   #2
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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, 05: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, 10:44 PM   #4
jefro
Guru
 
Registered: Mar 2008
Posts: 12,073

Rep: Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518
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 10:46 PM.
 
Old 06-10-2010, 11: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-11-2010, 12:19 AM   #6
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,156

Rep: Reputation: 327Reputation: 327Reputation: 327Reputation: 327
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-11-2010, 12:43 AM   #7
John VV
Guru
 
Registered: Aug 2005
Posts: 13,452

Rep: Reputation: 1799Reputation: 1799Reputation: 1799Reputation: 1799Reputation: 1799Reputation: 1799Reputation: 1799Reputation: 1799Reputation: 1799Reputation: 1799Reputation: 1799
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, 03:22 AM   #8
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
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, 04:59 PM   #9
jefro
Guru
 
Registered: Mar 2008
Posts: 12,073

Rep: Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518
That's what I am talking about.
 
Old 06-11-2010, 05: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, 05:42 PM   #11
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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, 06:45 PM   #12
jefro
Guru
 
Registered: Mar 2008
Posts: 12,073

Rep: Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518Reputation: 1518
Even better.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 01:10 AM
KDE and double clicking Ravo Linux - General 2 02-20-2005 12:41 PM
How to run a script by double clicking it tom221 Linux - Newbie 6 02-07-2005 04:04 PM
enable Double clicking??? xhispage Linux - Newbie 2 10-19-2004 04:22 AM
File Manager and Double-clicking Jongi Linux - Newbie 3 09-04-2003 11:28 AM


All times are GMT -5. The time now is 11:34 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration