03-31-2007
Part 2 -- The Details
The Format of the #! Line
We can say for certain that the first 2 characters must be "#!". Or can we? Many systems, it seems, are willing to delete leading white space. My recommendation is to start with #!. It's traditional.
Next there may be an optional space. Some documentation says this space is required but as far as anyone can determine the only Unix release to require the space was a snapshot release of BSD 4.1... this was not a general release). Actually, it appears that you may have several spaces if you want. And some testing with TAB characters has been done and seems to work. My recommendation is to stay with zero or one spaces.
Next comes the full path to the interpreter and like all full paths, it must start with a /. Oops, another exception... The Linux kernel (at least version 2.0.34) is willing to accept a relative path. My recommendation is don't do that.
We may be done. Or we may have optional white space which lead to our single argument. Except that some versions of FreeBSD handle multiple arguments.
Most versions of BSD and HP-UX will strip trailing white space. Other versions of Unix treat trailing white space as valid characters. And a few versions of BSD can accept a trailing comment delimited by a # character.
How long can the line be? A few versions of Unix set the limit as low as 32 characters. FreeBSD can apparently handle 8192 characters.
At least the line always ends with the Unix standard \n character, right? Well, not always. Some versions of Unix will tolerate a \r\n ending and strip off the \r while others won't do that.
This is not as standard as it could be...
Argument 0 of The Process, Not The Script
There is another way that implementations may differ. Consider the perl script that I ran ar the end of part 1. My shell did the approximate equivalent of
execl("./perlargs", "./perlargs", "one", "two", "three", (char *) NULL)
and the kernel transformed it into the approximate equivalent of
execl("/usr/local/bin/perl", "/usr/local/bin/perl", "-w", "./perlargs", "one", "two", "three", (char *) NULL)
Highlighted in red is argument zero which by convention is the same as the path of the program being executed. A notable execution is that the login program will set it to stuff like "-ksh". Originally, executable shell scripts had the argument 0 set to the name of the script rather than the name of the interpreter. These days, the name of the interpreter is common. The last hold-out I know of is HP-UX which sets argument 0 to the name of the script.
These 3 Users Gave Thanks to Perderabo For This Post:
8 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
Hi,
I have a developer that is trying to start a script with sh "scriptname". In the script, he is specifying #!/usr/bin/ksh as the command interpreter. For some reason sh is ignoring the #!/usr/bin/ksh. We are running Solaris 8. Does anyone have any ideas what could be causing this? Here... (3 Replies)
Discussion started by: ckeith79
3 Replies
2. UNIX for Dummies Questions & Answers
I am using IBM AIX unix version 4.3.3.0.
In a directory there are many files with different patterns.
When I am trying to execute the command, ls -l with the file pattern, which have fewer files it gives the desired result. However when I am trying to execute the same command for file pattern,... (2 Replies)
Discussion started by: jitindrabappa
2 Replies
3. Solaris
Hi all,
below is the problem details:
ora10g@CNORACLE1>which ld
/usr/ucb/ld
ora10g@CNORACLE1>cd /usr/ccs/bin
ora10g@CNORACLE1>ln -s /usr/ucb/ld ld
ln: cannot create ld: File exists
ora10g@CNORACLE1>
how to link it to /usr/ccs/bin? (6 Replies)
Discussion started by: SmartAntz
6 Replies
4. Shell Programming and Scripting
Usually we use !/usr/bin/ksh at the start of the script.But if I am having this stuff in the scripts and calling one script from other its not working.What may be the reason behind it ?
xyz.ksh
#!/usr/bin/ksh
echo "Hi"
abc.ksh
#!/usr/bin/ksh
echo "I am fine"
ksh xyz.ksh
Its... (4 Replies)
Discussion started by: dr46014
4 Replies
5. AIX
Hi,
I am getting the below error message When i am trying to delete the files from the directory.Could you please guide me?
rm *.aud
ksh: /usr/bin/rm: 0403-027 The parameter list is too long.
and
find /oracle/admin/testP/adump/*.aud -mtime +5 -exec rm {} \;
ksh: /usr/bin/find:... (3 Replies)
Discussion started by: nokiae63
3 Replies
6. OS X (Apple)
Q1. I understand that /usr/local/bin means I can install/uninstall stuff in here and have any chance of messing up my original system files or effecting any other users. I created this directory myself.
But what about the directory I didn't create, namely /Users/m/bin? How is that directory... (1 Reply)
Discussion started by: michellepace
1 Replies
7. Shell Programming and Scripting
I saw one script using the first line as below
/usr/bin/ksh -E
I have used -x for debug but couldn't find what is this -E option for ?
Pls let me know what is this -E used for
Thanks
RL (1 Reply)
Discussion started by: reldb
1 Replies
8. BSD
I'm not sure if this is the default behavior for the ld command, but it does not seem to be looking in /usr/local/lib for shared libraries.
I was trying to compile the latest version of Kanatest from svn. The autorgen.sh script seems to exit without too much trouble:
$ ./autogen.sh
checking... (2 Replies)
Discussion started by: AntumDeluge
2 Replies
shift(1) User Commands shift(1)
NAME
shift - shell built-in function to traverse either a shell's argument list or a list of field-separated words
SYNOPSIS
sh
shift [n]
csh
shift [variable]
ksh
* shift [n]
DESCRIPTION
sh
The positional parameters from $n+1 ... are renamed $1 ... . If n is not given, it is assumed to be 1.
csh
The components of argv, or variable, if supplied, are shifted to the left, discarding the first component. It is an error for the variable
not to be set or to have a null value.
ksh
The positional parameters from $n+1 $n+1 ... are renamed $1 ..., default n is 1. The parameter n can be any arithmetic expression that
evaluates to a non-negative number less than or equal to $#.
On this man page, ksh(1) commands that are preceded by one or two * (asterisks) are treated specially in the following ways:
1. Variable assignment lists preceding the command remain in effect when the command completes.
2. I/O redirections are processed after variable assignments.
3. Errors cause a script that contains them to abort.
4. Words, following a command preceded by ** that are in the format of a variable assignment, are expanded with the same rules as a vari-
able assignment. This means that tilde substitution is performed after the = sign and word splitting and file name generation are not
performed.
ATTRIBUTES
See attributes(5) for descriptions of the following attributes:
+-----------------------------+-----------------------------+
| ATTRIBUTE TYPE | ATTRIBUTE VALUE |
+-----------------------------+-----------------------------+
|Availability |SUNWcsu |
+-----------------------------+-----------------------------+
SEE ALSO
csh(1), ksh(1), sh(1), attributes(5)
SunOS 5.10 15 Apr 1994 shift(1)