Softlink invoke and working directory problems of Linux shell programming

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.