What does "#! /usr/bin/ksh" mean?


QUESTION:


Whenever we write a shell script the first statement would be #! /usr/bin/kshor something of the same..

What does this signify??? I know # is a comment.. what is this ! for???

Give in your thoughts....


Thanks,
Nisha


ANSWER:

1.

It's to tell your shell what shell to you in executing the following statements in your shell script.

Yes, that is it. But, this kind of comment should be in your first line of the script, only then does it take this form. Also, when your shell comes across this line, a new process is spawned and replaced with this shell of your choice, and it is this process which will parse your script.

2.

On the first line of an interpreter script, the "#!", is the name of a program which should be used to interpret the contents of the file. For instance, if the first line contains "#! /bin/sh", then the contents of the file are executed as a shell script.

You can get away without this, but you shouldn't. All good scripts state the interpretor explicitly. The '!' is also known as intepreter for the script to recognise correct compiler to be used.

3.by Perderabo (不错的解释)

This will probably be more than you wanted to know, but here goes anyway....

Originally, we only had one shell on unix. When you asked to run a command, the shell would attempt to invoke one of the exec() system calls on it. It the command was an executable, the exec would succeed and the command would run. If the exec() failed, the shell would not give up, instead it would try to interpet the command file as if it were a shell script.

Then unix got more shells and the situation became confused. Most folks would write scripts in one shell and type commands in another. And each shell had differing rules for feeding scripts to an interpreter.

This is when the "#! /" trick was invented. The idea was to let the kernel's exec() system calls succeed with shell scripts. When the kernel tries to exec() a file, it looks at the first 4 bytes which represent an integer called a magic number. This tells the kernel if it should try to run the file or not. So "#! /" was added to magic numbers that the kernel knows and it was extended to actually be able to run shell scripts by itself. But some people could not type "#! /", they kept leaving the space out. So the kernel was exended a bit again to allow "#!/" to work as a special 3 byte magic number.
So
#! /usr/bin/ksh
and
#!/usr/bin/ksh
now mean the same thing. I always use the former since at least some kernels might still exist that don't understand the latter.

And note that the first line is a signal to the kernel, and not to the shell. What happens now is that when shells try to run scripts via exec() they just succeed. And we never stumble on their various fallback schemes.


QUESTION 2:

Thanks Perderabo. But what if i dont give this statement called magic numbers in my shell...

Anything wrong in it.. it almost looks like a coding standard for the script that we write...

Thanks,
Nisha


ANSWER 2:

If you leave it out, then the shell will fail when it tries to exec() it. What happens next depends on which shell you are using as your login shell. Most shells will assume that the script is written in their own language. So if you are using bash as your login shell and try to run a korn shell script that didn't have the #! /usr/bin/ksh, then bash will try to run it as a bash script. bash and ksh are somewhat compatible and this might work. On the other hand, it might fail.

Suppose you use ksh as your login shell and you are running korn shell scripts without that first line. Things seem ok. Then you decide to try bash as your login shell. Suddenly things break.

And things will be especially grim for anyone who uses csh/tcsh as their login shell and runs scripts without that first line. These shells inspect the script to try and guess whether they or the old bourne shell should run it. The test they employ is so bad that it can be beaten by a random number generator. And no one still uses the old bourne shell anyway.

And yes, it is sort of a standard. Most kernels know about it. But I don't believe that is an officialposix standard.



摘自:http://www.unix.com/answers-frequently-asked-questions/7077-what-does-usr-bin-ksh-mean.html点击打开链接



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值