我有shell脚本,它以:
sdir=`dirname $0`
sdir=`(cd "$sdir/"; pwd)`
Run Code Online (Sandbox Code Playgroud)
这通常会扩展(使用'sh -h')
++ dirname /opt/foo/bin/bar
+ sdir=/opt/foo/bin
++ cd /opt/foo/bin/
++ pwd
+ sdir=/opt/foo/bin
Run Code Online (Sandbox Code Playgroud)
但是对于单个用户来说,扩展成单个参数组合(注意结果sbin值的两行)
++ dirname bin/foo
+ sdir=bin
++ cd bin/
++ pwd
+ sdir='/opt/foo/bin
/opt/foo/bin'
Run Code Online (Sandbox Code Playgroud)
我尝试了不同的组合,但无法重现此行为.通过该用户的不同输入参数,它开始产生正确的单行结果.我是shell脚本的新手,所以请注意这样(cd X; pwd)可以返回两行.它是在CentOS上观察到的,但不确定它是否重要.请指教.
罪魁祸首是cd,试试这个
sdir=`dirname $0`
sdir=`(cd "$sdir/" >/dev/null; pwd)`
Run Code Online (Sandbox Code Playgroud)
发生这种情况是因为当您指定非绝对路径并且在环境变量CDPATH中找到该目录时,cd会将stdout输出到其更改为的目录的绝对路径的值.
相关人员bash部分:
CDPATH The search path for the cd command. This is a
colon-separated list of directories in which the
shell looks for destination directories specified
by the cd command. A sample value is ``.:~:/usr''.
cd [-L|-P] [directory]
Change the current working directory to directory. If
directory is not given, the value of the HOME shell
variable is used. If the shell variable CDPATH exists,
it is used as a search path. If directory begins with a slash,
CDPATH is not used.
The -P option means to not follow symbolic links; symbolic
links are followed by default or with the -L option. If
directory is ‘-’, it is equivalent to $OLDPWD.
If a non-empty directory name from CDPATH is used, or if ‘-’
RELEVANT -\ is the first argument, and the directory change is successful,
PARAGRAPH -/ the absolute pathname of the new working directory is written
to the standard output.
The return status is zero if the directory is successfully
changed, non-zero otherwise.
OLDPWD The previous working directory as set by the cd
command.
| 归档时间: |
|
| 查看次数: |
346 次 |
| 最近记录: |