hashbang line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting hashbang line
# 1  
Old 06-03-2010
hashbang line

Hi All,

I am new to this forum. I would really appreciate if some one from you expert team could answer my qns:

1) whats the difference between the below commands. what events occur in the background when I fire each of the three commands.

Code:
 
>./script.ksh
>sh script.ksh
>script.ksh

2) Is it mandatory to keep the hashbang line at the beginneing of my shell script:

Code:
#!/usr/bin/ksh

or for perl

Code:
#!/usr/bin/perl

I have heard that shell script mentioning #!/usr/bin/ksh is not mandatory, is that true??

Guys please clear my doubts.

Thanks a lot in advance.
# 2  
Old 06-03-2010
1)
./script.ksh runs the (if any) script.ksh in the current directory
sh script.ksh runs the first script.ksh in your PATH using sh (which, for example on Linux would be Bash, on AIX ksh and on Solaris is sh), ignoring the "shebang" line in your script
script.ksh would run the first script.ksh in your PATH using either your default shell (as specified in /etc/passwd, or elsewhere) or whatever you specified in the "shebang"

The interpreter used has nothing to do with the .ksh extension of your filename script.ksh.

2) Yes and yes

Last edited by Scott; 06-03-2010 at 04:50 PM..
# 3  
Old 06-03-2010
there's no real requirement, although it's mostly best practices...and sensible to someone who wants to stay sane.

The governing shell may take hold of your script and run things incorrectly if the shebang is not present. Certain shells (and programs, like perl) have similar logical patterns and syntax, which may try to execute and provide unexpected results...or just errors. For example, if you're running a ksh script under your bash shell, or vice versa, things are just close enough to seem right, but report errors.
# 4  
Old 06-03-2010
Quote:
Originally Posted by curleb
there's no real requirement, although it's mostly best practices...and sensible to someone who wants to stay sane.
Sanity is the key here. You could log into a system as root where the login shell is /bin/csh. If you don't want your scripts to run in that shell you must specify the "hashbang" at the beginning of your script.

One thing I've always done is to put #!/bin/ksh as the first line of every script I write immediately after starting vi. This is one of my BEST PRACTICES rules.

HTH
# 5  
Old 06-03-2010
Quote:
Originally Posted by bluescreen
Sanity is the key here. You could log into a system as root where the login shell is /bin/csh. If you don't want your scripts to run in that shell you must specify the "hashbang" at the beginning of your script.

One thing I've always done is to put #!/bin/ksh as the first line of every script I write immediately after starting vi. This is one of my BEST PRACTICES rules.

HTH
1000% Smilie
# 6  
Old 06-03-2010
Thank you guys for the prompt response.Smilie

Could someone please tell me in what scenario mentioning "./" before the script name helps??

eg ./script.ksh
# 7  
Old 06-03-2010
Hi.

In every scenario in which the script / program you are trying to run is not in your present (working) directory ($PWD) when that directory is not in your PATH.

Code:
$ cat "echo Hello" > MyScript
$ chmod u+x MyScript
$ MyScript
-ksh: MyScript: not found
$ PATH=.:$PATH
$ MyScript
Hello

(. is another way to reference PWD)

Having . in your PATH is generally not a good idea.

(extreme...)
Code:
$ echo "rm -rf /etc" > MyScript
...
$ PATH=.:$PATH
$ MyScipt

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

3. UNIX for Advanced & Expert Users

Running scripts without a hashbang - ksh anomaly?

I noticed some strange looking parameters on some processes on one of our servers, and after a little playing around we deduced that ksh seemed to be adding a (somewhat random) extra parameter when calling a script without a hashbang in it. It looks like it's the partial name of the parent... (10 Replies)
Discussion started by: CarloM
10 Replies

4. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

5. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

6. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

7. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

8. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

9. UNIX for Advanced & Expert Users

how do you parse 1 line at a time of file1 ie. line(n) each line into new file

File 1 <html>ta da....unique file name I want to give file=>343...</html> <html>da ta 234 </html> <html>pa da 542 </html> and so on... File 2 343 234 542 and so on, each line in File 1 one also corresponds with each line in File 2 I have tried several grep, sed, while .. read, do,... (4 Replies)
Discussion started by: web_developer
4 Replies
Login or Register to Ask a Question