关于两种运行sh的方式: ./myscript.ksh and . ./myscript.ksh

来源:互联网 发布:k均值是关联规则算法 编辑:程序博客网 时间:2024/04/28 16:26

这个问题困扰我很久很久了。。。有时候用这个就行,有时候用那个才行,到底有什么区别?

Difference Between executing like ./myscript.ksh and . ./myscript.ksh (两个点之间有空格)

我参考了下面这两篇帖子:

http://www.unix.com/unix-dummies-questions-answers/156460-difference-between-executing-llike-myscript-ksh-myscript-ksh.html

http://publib.boulder.ibm.com/infocenter/zvm/v5r4/index.jsp?topic=/com.ibm.zvm.v54.dmsp3/comd672.htm


. myscript.ksh的意思是,Run a shell file in the current environment

也就是说executes ksh in current shell.  Since it's run in the current shell, any variables it sets, for example, are available in your current shell.

但我们常用的形式是. ./myscript.ksh (中间有空格). 这是因为文件名的缘故:

If there are slashes in the file name, . (dot) looks for the named file. If there are no slashes. (dot) uses the search PATH variableto findfile.

This may surprise some people when they use dot torun a file in the working directory, but their search rules are not set upto look at the working directory. As a result, the shell doesn't find theshell file. If you have this problem, you can use: 

. ./myscript.ksh

因此上句的意思就是,使用current environment运行这个脚本,同时,在工作目录搜索该脚本。


所以现在很清楚了, ./myscript.ksh的意思是执行当前工作目录中的这个脚本,但是是在不同的shell中。


二者的不同会导致一个明显的区别:

如果在一个ksh中设置了一些变量,然后调用了:

ksh `. ./myscript.ksh` 那么之前设置的变量可以在子脚本,即myscript.ksh中使用。

如果调用的是:

ksh `./myscript.ksh` 那么之前设置的变量不能在myscript.ksh中使用。




原创粉丝点击