Get shell script share _linux Shell in directory of the shell script itself

Source: Internet
Author: User
Tags symlink

A few days ago wrote seven bull demo, with Bash wrote a convenient installation of the script, involving the path-related judgments, from StackOverflow, plus their own practice to tidy up.

Simple version

The following is one of the simplest implementations that can solve most of the problems that are displayed for soft links in the directory where the soft links are located

Lineos:false

Copy Code code as follows:

#!/bin/bash
Dir= "$ (CD" $ (dirname "$") "&& pwd"

Perfect version

This release solves the problem of using ln-s target linkname to create soft links that do not correctly take the real script.

Lineos:false

Copy Code code as follows:

#!/bin/bash
Source= "$"
While [-H "$SOURCE"]; Do # resolve $SOURCE until the ' file is no longer a symlink
Dir= "$ (cd-p" $ (dirname "$SOURCE") "&& pwd)"
Source= "$ (readlink" $SOURCE ")"
[[$SOURCE!=/*]] && source= "$DIR/$SOURCE" # If $SOURCE is a relative symlink, we need to resolve it relative To the path where the Symlink file is located
Done
Dir= "$ (cd-p" $ (dirname "$SOURCE") "&& pwd)"

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.