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

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

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 find file.

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中使用。




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值