LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 779|回复: 12

如何得到正在执行的脚本的当前目录

[复制链接]
发表于 2004-10-20 14:58:34 | 显示全部楼层 |阅读模式
当在执行一个脚本时,如何得到这个正在执行的脚本目录?

谢谢
发表于 2004-10-20 15:17:55 | 显示全部楼层
dirname可以吗?
发表于 2004-10-21 01:48:57 | 显示全部楼层
dirname $0
我记得是这个。
发表于 2004-10-21 08:55:05 | 显示全部楼层
script 里加上pwd也行啊.
 楼主| 发表于 2004-10-21 15:35:09 | 显示全部楼层
thanks.

dirname works fine
发表于 2004-10-21 19:50:30 | 显示全部楼层
echo $PWD
发表于 2004-10-21 19:57:05 | 显示全部楼层
you will be superised if you running /tmp/myScript.sh in another directory. . I don't think $PWD will return the correct answer
 楼主| 发表于 2004-11-1 13:28:25 | 显示全部楼层
Thanks for your reply.

How to set a variable in script and make it aviable after exit the script.
发表于 2004-11-2 17:45:30 | 显示全部楼层
To 黯然销魂:

我的方法如下:

workingFolder="`dirname $0`"
cd $workingFolder
workingFolder="`pwd`"
echo $workingFolder

$workingFolder是脚本所在目录的绝对路径。
发表于 2004-11-4 12:20:45 | 显示全部楼层
最初由 cnspy 发表
Thanks for your reply.

How to set a variable in script and make it aviable after exit the script.


run the scirpt in this way:

. ./myScript.sh

with a dot in front.

get more information from "man ."
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表