免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2310 | 回复: 5
打印 上一主题 下一主题

[请教]如何理解"."用作shell替换? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-01-15 11:04 |只看该作者 |倒序浏览
10可用积分
我用putty连接的远程机器
<<UNIX实例精解>>教材上面说如果在shell(bsh,ksh)运行的过程中
. [一个程序]

然后当前shell的进程就会被这个程序替代,程序退出后,当前的shell也退出了。

所以我预测它运行完毕以后可能回到更上级的shell,也可能让putty退出(如果当前的shell已经处于顶层)。但是我做的实验似乎不是这样的结果。

>echo $SHELL
ksh
$ cat kg.sh
echo haha
ls
$ . ./kg.sh  ->注意这里有两个"." 我期待的是第一个.导致当前shell被后面程序替换
haha
downloads
programs
$

当前的shell并没有退出或者被kg.sh取代,我在kg.sh的第一行加入#!/bin/sh或者#!/bin/ksh也是同样的结果。和我直接./kg.sh也是一样的结果。

是我的理解有误么? 还请dx指点,10分感谢!

最佳答案

查看完整内容

我觉得可以把.理解为把后面的文件里的东西一行行贴出来在命令行上执行以下如果. filename是在脚本里可以理解为把filename里的内容插入到脚本里的.filename那一行

论坛徽章:
0
2 [报告]
发表于 2009-01-15 11:04 |只看该作者
我觉得可以把.

理解为把后面的文件里的东西一行行贴出来在命令行上执行以下

如果. filename是在脚本里
可以理解为把filename里的内容插入到脚本里的.filename那一行

论坛徽章:
0
3 [报告]
发表于 2009-01-15 11:11 |只看该作者
你的理解有误
要让putty退出
$ cat kg.sh
echo haha
ls
exit
       source filename [arguments]
              Read and execute commands from filename in the current shell environment and return the exit  status  of  the  last
              command  executed  from  filename.   If  filename does not contain a slash, file names in PATH are used to find the
              directory containing filename.  The file searched for in PATH need not be executable.  When bash is  not  in  posix
              mode, the current directory is searched if no file is found in PATH.  If the sourcepath option to the shopt builtin
              command is turned off, the PATH is not searched.  If any arguments are supplied, they become the positional parame-
              ters  when filename is executed.  Otherwise the positional parameters are unchanged.  The return status is the sta-
              tus of the last command exited within the script (0 if no commands are executed), and  false  if  filename  is  not
              found or cannot be read.

[ 本帖最后由 我是DBA 于 2009-1-15 11:15 编辑 ]

论坛徽章:
9
2015亚冠之阿尔纳斯尔
日期:2015-09-10 16:21:162015亚冠之塔什干火车头
日期:2015-07-01 16:23:022015年亚洲杯之巴勒斯坦
日期:2015-04-20 17:19:46子鼠
日期:2014-11-13 09:51:26未羊
日期:2014-08-28 18:13:36技术图书徽章
日期:2014-02-21 09:30:15酉鸡
日期:2014-01-14 11:12:49天蝎座
日期:2013-12-09 17:56:53平安夜徽章
日期:2015-12-26 00:06:30
4 [报告]
发表于 2009-01-15 11:11 |只看该作者
.等同于source
你说的应该是exec命令
eg: exec ./kg.sh

论坛徽章:
0
5 [报告]
发表于 2009-01-15 11:19 |只看该作者
同意楼上的
.  记得好像是载入文件到内存中,可将当前文件内容作当前SHELL的环境变量
exec 执行时不创建子SHELL,所以执行完命令后就退出当前SHELL了

论坛徽章:
0
6 [报告]
发表于 2009-01-15 18:45 |只看该作者
原帖由 welcome008 于 2009-1-15 11:27 发表
我觉得可以把.

理解为把后面的文件里的东西一行行贴出来在命令行上执行以下

如果. filename是在脚本里
可以理解为把filename里的内容插入到脚本里的.filename那一行

这个解释很好,给分了!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP