dirname:
帮助文档内容如下。
Usage: dirname [OPTION] NAME...
Output each NAME with its last non-slash component and trailing slashesremoved; if NAME contains no /'s, output '.' (meaning the current directory).
-z, --zero separate output with NUL rather than newline
--help display this help and exit
--version output version information and exit
Examples:
dirname /usr/bin/ -> "/usr"
dirname dir1/str dir2/str -> "dir1" followed by "dir2"
dirname stdio.h -> "."
常用方法:
在命令行状态下单纯执行 $ cd `dirname $0` 是毫无意义的。因为他返回当前路径的"."。
这个命令写在脚本文件里才有作用,他返回这个脚本文件放置的目录,并可以根据这个目录来定位所要运行程序的相对位置(绝对位置除外)。
在/home/admin/test/下新建test.sh内容如下:
- cd `dirname $0`
- echo `pwd`
然后返回到/home/admin/执行
- sh test/test.sh
运行结果:
- /home/admin/test
这样就可以知道一些和脚本一起部署的文件的位置了,只要知道相对位置就可以根据这个目录来定位,而可以不用关心绝对位置。这样脚本的可移植性就提高了