Linux Shell下获取当前脚本的绝对路径
2016-01-05 11:27
615 查看
错误的做法
使用pwd
使用0
改进的建议
NAME
pwd - print name of current/working directory
SYNOPSIS
pwd [OPTION]…
DESCRIPTION
Print the full filename of the current working directory.
-L, –logical
use PWD from environment, even if it contains symlinks
-P, –physical
avoid all symlinks
–help
display this help and exit
–version
output version information and exit
NOTE: your shell may have its own version of pwd, which usually supersedes the version described here. Please refer to your shell’s documentation for details about the options it supports.
但无论如何,谁也无法保证当前工作目录就是脚本存放的目录。正如这样:
Expands to the name of the shell or shell script. This is set at shell initialization. If bash is invoked with a file of commands, $0 is set to the name of that file. If bash is started with the -c option, then $0 is set to the first argument after the string to be executed, if one is present. Otherwise, it is set to the file name used to invoke bash, as given by argument zero.
我们来举例说明一下通常情况下它的返回值,如你所见这个
如此说来,只有dot execute或是说source执行脚本时(上例中第三种执行方式),
参考文献:
1. http://www.cnblogs.com/FlyFive/p/3640267.html
使用pwd
使用0
改进的建议
错误的做法
使用pwd
一种很常见的错误做法是使用pwd命令,这个命令的作用是“print name of current/working directory”,因此当脚本执行过程中改变了工作目录,这个pwd命令返回的结果也将随之改变。NAME
pwd - print name of current/working directory
SYNOPSIS
pwd [OPTION]…
DESCRIPTION
Print the full filename of the current working directory.
-L, –logical
use PWD from environment, even if it contains symlinks
-P, –physical
avoid all symlinks
–help
display this help and exit
–version
output version information and exit
NOTE: your shell may have its own version of pwd, which usually supersedes the version described here. Please refer to your shell’s documentation for details about the options it supports.
但无论如何,谁也无法保证当前工作目录就是脚本存放的目录。正如这样:
[chen@bwvps1 ~]$ cat ./dir1/dir2/using_pwd.sh #!/usr/bin/env bash echo 'pwd: ' $(pwd) [chen@bwvps1 ~]$ using_pwd.sh pwd: /home/chen [chen@bwvps1 ~]$ ./dir1/dir2/using_pwd.sh pwd: /home/chen [chen@bwvps1 ~]$ . ./dir1/dir2/using_pwd.sh pwd: /home/chen
使用$0
另一个常见的误区是使用$0,从
man bash中可以读到关于它的解释:
Expands to the name of the shell or shell script. This is set at shell initialization. If bash is invoked with a file of commands, $0 is set to the name of that file. If bash is started with the -c option, then $0 is set to the first argument after the string to be executed, if one is present. Otherwise, it is set to the file name used to invoke bash, as given by argument zero.
我们来举例说明一下通常情况下它的返回值,如你所见这个
$0要比
pwd复杂得多:
[chen@bwvps1 ~]$ cat ./dir1/dir2/using_dzero.sh #!/usr/bin/env bash echo '$0: ' $0 [chen@bwvps1 ~]$ using_dzero.sh $0: /home/chen/dir1/dir2/using_dzero.sh [chen@bwvps1 ~]$ ./dir1/dir2/using_dzero.sh $0: ./dir1/dir2/using_dzero.sh [chen@bwvps1 ~]$ . ./dir1/dir2/using_dzero.sh $0: -bash
如此说来,只有dot execute或是说source执行脚本时(上例中第三种执行方式),
$0不能返回脚本所在的路径,其他情况下得到的只是绝对路径和相对路径的分别,因此配合
dirname $0就可以解决大多数问题。
改进的建议
当然,相对于每次纠结绝对路径还是相对路径来说,我们有一个更好的办法,可以直接获取到脚本的绝对路径。于是我们对上面的方法进行了改进,将获取方式改为$(cd $(dirname $0); pwd)。最终的结果如我们所见:
[chen@bwvps1 ~]$ cat dir1/dir2/test.sh #!/usr/bin/env bash echo 'pwd: ' $(pwd) echo '$0: ' $0 BASENAME=$(cd $(dirname $0); pwd) echo "\$BASENAME: " $BASENAME [chen@bwvps1 ~]$ test.sh pwd: /home/chen $0: /home/chen/dir1/dir2/test.sh $BASENAME: /home/chen/dir1/dir2 [chen@bwvps1 ~]$ ./dir1/dir2/test.sh pwd: /home/chen $0: ./dir1/dir2/test.sh $BASENAME: /home/chen/dir1/dir2 [chen@bwvps1 ~]$ . ./dir1/dir2/test.sh pwd: /home/chen $0: -bash dirname: invalid option -- 'b' Try 'dirname --help' for more information. $BASENAME: /home/chen
参考文献:
1. http://www.cnblogs.com/FlyFive/p/3640267.html
相关文章推荐
- android wifi 无线调试
- 运维入门
- 动态清空 nohup 输出文件
- install scrapy with pip and easy_install
- Linux Shell常用技巧
- Shell 脚本编程陷阱
- VBS脚本写的Windows硬件检测工具分享
- 用vbscript实现隐藏任务栏图标的脚本
- 用autoit编写第一个脚本(Hello World)
- VBS调用WMI快速关闭IE的脚本
- Oracle数据库执行脚本常用命令小结
- 收集的ROS防火墙脚本
- JSP脚本漏洞面面观
- shell字符串操作详解
- 不错的批处理脚本 第一部分
- VBS脚本加密/解密VBS脚本(简易免杀版1.1)
- 不错的批处理脚本实例代码 第二部分
- 使用脚本和批处理清除电脑中的痕迹的代码第1/2页