LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 11-19-2010, 05:59 AM   #1
kebabbert
Member
 
Registered: Jul 2005
Posts: 463

Rep: Reputation: 41
How to execute a shell script (with source command)????


This is weird.

I have a shell script with no execute rights.
$ chmod -x test.sh

Then I try
$ test.sh
which does not work. (I have "." in PATH)

When I do
$ . test.sh
it works! I can run the script even though I have no execute rights. Why is that?



Another question: If I have a shell script without a hash bang, I still can execute the shell script. Why? What does hash bang do? If there is no hash bang, why is the shell script run? What does the hash bang do?

#!/bin/sh <------- hash bang without this line, I can still run it!
cd /
 
Old 11-19-2010, 06:52 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
if you run "test.sh" then it searches your path for the script file. Your current directory is NOT on your path, adn should never be, so you should be running "./test.sh" instead. This is the proper way to do it.

with source, it does not run the script. It instead takes the text contents of the file and applies it to the current environment, which erm./.. runs it... but it's logically different, no shell is spawned, and each line is just blindly executed in order. in your example at the bottom there, sourcing it will leave you in the "/" directory. If you run it properly as a script then *within* the spawned shell you would change directory, but upon the script finishing, you would be returned to where you were. You can use "export" to make these things persist after leaving the script, but in general a script should not affect your login environment without explicit direction to do so.

so... "./test.sh"

Last edited by acid_kewpie; 11-19-2010 at 06:54 AM.
 
Old 11-19-2010, 05:54 PM   #3
kebabbert
Member
 
Registered: Jul 2005
Posts: 463

Original Poster
Rep: Reputation: 41
Ok, great! So, "source" just takes the text and runs each line inside the current shell. Thanks.

The 2nd question: If I write a shell script WITHOUT a hash bang, what happens? If I remove the first line, then bash does not know it is a shell script. So what happens?

What does the hash bang do? What happens if I omit the hash bang, then it is no shell script.
 
Old 11-19-2010, 06:07 PM   #4
honeybadger
Member
 
Registered: Aug 2007
Location: India
Distribution: Slackware (mainly) and then a lot of others...
Posts: 855

Rep: Reputation: Disabled
To be honest I have written shell scripts without hash bang and I did not make amy difference. I think hash bang is more of a convention.
 
Old 11-19-2010, 06:11 PM   #5
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Believe it or not, there's a pretty good WP article about shebangs, here:
http://en.wikipedia.org/wiki/Shebang_%28Unix%29

Interesting stuff, worth a read.
 
Old 11-19-2010, 09:59 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Quote:
Originally Posted by GrapefruiTgirl View Post
Believe it or not, there's a pretty good WP article about shebangs ...
Thanks for the heads up Sasha; an informative read

The execve man page does not say the space after #! is optional.
 
Old 11-21-2010, 07:07 AM   #7
kebabbert
Member
 
Registered: Jul 2005
Posts: 463

Original Poster
Rep: Reputation: 41
Ive read the wikipedia article. It does not really answer my question.

I have googled for this question a bit, and Ive read what the shell does with a file:

1) If the shell sees an executable file, it runs it.
2) If the shell sees a hash bang, it knows the file is a script so it runs it
3) If it is not an executable, nor has a hash bang - *some* systems treats the file as a script so it is run.

The question is nr 3). What happens exactly? What does "some systems" mean? What do other systems do? Refuse to run it?
 
Old 11-21-2010, 07:26 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
The file man page has a description of how the system determines the type of files.
 
Old 11-21-2010, 08:52 AM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
AFAIK it's the OS, not the shell, that looks at the shebang line. When the OS sees that the executable begins with "#!", it assumes that everything from that to the next newline is a path to an interpreter, and executes the interpreter with the filename as the argument instead of executing the file itself.

For example, when you begin a file with "#!/usr/bin/perl", it's run by the Perl interpreter.

Or, if you begin a file with "#!/bin/rm", the file will be deleted when you execute it!
 
Old 11-21-2010, 12:06 PM   #10
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,541

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
http://tldp.org/LDP/abs/html/sha-bang.html says
Quote:
#! can be omitted if the script consists only of a set of generic system commands, using no internal shell directives.
and

Quote:
If Bash is your default shell, then the #! isn't necessary at the beginning of a script. However, if launching a script from a different shell, such as tcsh, then you will need the #!.
Which seems to suggest if there is no #! line then your current shell will be invoked. I couldn't find any mention of this in more "official" documentation though.
 
  


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 execute Source command in a shell script UltraSoul Linux - General 3 09-23-2010 11:13 AM
[SOLVED] Source or Execute an external Script from shell script helptonewbie Linux - Newbie 2 08-11-2010 01:48 PM
[shell script] execute command and parse output stoiss Programming 2 01-26-2009 02:49 AM
Not able to execute Oracle Sql command in shell script Ranvijaidba Programming 3 10-23-2008 04:45 PM
How to execute a command line in a shell script orgazmo Programming 5 05-09-2005 10:16 AM


All times are GMT -5. The time now is 11:45 PM.

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