2011-05-02 74 views

回答

9
#!/bin/bash 
BASEDIR="$(dirname "$0")" 
cd "$BASEDIR" 
+0

这个作品谢谢! – olilarkin 2011-05-02 21:41:48

1

你必须使用引号:

#!/bin/bash 
BASEDIR=$(dirname $0) 
cd "$BASEDIR" 
+0

仍然无法正常工作,问题是与目录名称:使用:dirname路径 – olilarkin 2011-05-02 11:19:53

+0

检查$ PWD是否匹配$ 0路径。这可能会在basedir – hmontoliu 2011-05-02 11:37:36

+0

中产生空的结果。您还需要围绕$ 0组件引用引号,以便将它作为单个参数显示为dirname而不是多个参数。 – janm 2011-05-02 12:54:29

1

我使用这个开始我plackup服务器,我的: “run.command”

DIR=`dirname "$0"` 
cd "$DIR" 
plackup -r 
0

这可能是一个愚蠢的。以前的答案都不会解决符号链接问题。 这应该是合理的便携式(双重检查与破折号和bash),并会遍历与空格符号链接路径(S):

#!/bin/sh # dash bash ksh # !zsh (issues). G. Nixon, 12/2013. Public domain. 

## 'linkread' or 'fullpath' or (you choose) is a little tool to recursively 
## dereference symbolic links (ala 'readlink') until the originating file 
## is found. This is effectively the same function provided in stdlib.h as 
## 'realpath' and on the command line in GNU 'readlink -f'. 

##===-------------------------------------------------------------------===## 

for argv; do :; done # Last parameter on command line, for options parsing. 

## Error messages. Use functions so that we can sub in when the error occurs. 

recurses(){ printf "Self-referential:\n\t$argv ->\n\t$argv\n" ;} 
dangling(){ printf "Broken symlink:\n\t$argv ->\n\t"$(readlink "$argv")"\n" ;} 
errnoent(){ printf "No such file: "[email protected]"\n" ;} # Borrow a horrible signal name. 

# Probably best not to install as 'pathfull', if you can avoid it. 

pathfull(){ cd "$(dirname "[email protected]")"; link="$(readlink "$(basename "[email protected]")")" 

## 'test and 'ls' report different status for bad symlinks, so we use this. 

if [ ! -e "[email protected]" ]; then if $(ls -d "[email protected]" 2>/dev/null) 2>/dev/null; then 
    errnoent 1>&2; exit 1; elif [ ! -e "[email protected]" -a "$link" = "[email protected]" ]; then 
    recurses 1>&2; exit 1; elif [ ! -e "[email protected]" ] && [ ! -z "$link" ]; then 
    dangling 1>&2; exit 1; fi 
fi 

## Not a link, but there might be one in the path, so 'cd' and 'pwd'. 

if [ -z "$link" ]; then if [ "$(dirname "[email protected]" | cut -c1)" = '/' ]; then 
    printf "[email protected]\n"; exit 0; else printf "$(pwd)/$(basename "[email protected]")\n"; fi; exit 0 
fi 

## Walk the symlinks back to the origin. Calls itself recursivly as needed. 

while [ "$link" ]; do 
    cd "$(dirname "$link")"; newlink="$(readlink "$(basename "$link")")" 
    case "$newlink" in 
    "$link") dangling 1>&2 && exit 1          ;; 
     '') printf "$(pwd)/$(basename "$link")\n"; exit 0     ;; 
      *) link="$newlink" && pathfull "$link"       ;; 
    esac 
done 
printf "$(pwd)/$(basename "$newlink")\n" 
} 

## Demo. Install somewhere deep in the filesystem, then symlink somewhere 
## else, symlink again (maybe with a different name) elsewhere, and link 
## back into the directory you started in (or something.) The absolute path 
## of the script will always be reported in the usage, along with "$0". 

if [ -z "$argv" ]; then scriptname="$(pathfull "$0")" 

# Yay ANSI l33t codes! Fancy. 
printf "\n\033[3mfrom/as: \033[4m$0\033[0m\n\n\033[1mUSAGE:\033[0m " 
printf "\033[4m$scriptname\033[24m [ link | file | dir ]\n\n   " 
printf "Recursive readlink for the authoritative file, symlink after " 
printf "symlink.\n\n\n   \033[4m$scriptname\033[24m\n\n  " 
printf " From within an invocation of a script, locate the script's " 
printf "own file\n   (no matter where it has been linked or " 
printf "from where it is being called).\n\n" 

else pathfull "[email protected]" 
fi