Softlink is also called symbolic link, the equivalent of a "shortcut", by Ln–s sourcefile distinctionfile (ln–s TARGET link_name) created, the existence of the connection file makes the file system simplified, Users are more convenient to operate. But in shell programming is a very important point, because of the working directory, the shell needs to use "standard" variables (similar to the system environment variables) to locate its current working directory and the files in these directories, if the working directory definition is wrong , the shell script is expected to be difficult to run correctly. For example, the shell script uses $ (PWD) or other relative paths instead of absolute paths, which is strange! Because the existence of softlink can cause some bad-writing shell scripts to be referenced or executed in a softlink way, there is a big problem, in order to avoid this problem, you need to parse Softlink into a real file, or get a real working directory.
The workaround is as follows (refer to Tomcat's catalina.sh script):
# Resolve Links-Softlink-stolen is a from catalina.sh prg= "$" while [-H "$PRG"]; Do ls= ' ls-ld ' $PRG "' link= ' expr" $ls ": ' .*-> \ (. *\) $ ' If expr ' $link ': '/.* ' >/dev/null; Then prg= "$link" Else prg= ' dirname "$PRG" '/"$link" fi do prgdir= ' dirname "$PRG" '
After processing, the working directory can be set to Prgdir to solve the problem of working directory, application example:
#!/bin/bash # resolve links - $0 may be a Softlink prg= "$" while [ -h "$PRG" ]; do ls= ' ls -ld "$PRG" ' link= ' expr "$ls" : ". *-> \ (. *\) $" if expr "$link" : '/.* ' > /dev/null; then Prg= "$link" else prg= ' dirname ' $PRG '/' $link ' fi done# get standard environment variables prgdir= ' DirName "$PRG" ' # public header workdir= $PRGDIR # where to get source code sourceurl= # end public Header # -public headerfunction deploy () {
--end--
This article is from "Communication, My Favorites" blog, please make sure to keep this source http://dgd2010.blog.51cto.com/1539422/1676488
Softlink invoke and working directory problems of Linux shell programming