Browse Source

feat: add canonical readlink

main
Jan Lehnardt 2 years ago
parent
commit
e9e8944c18
3 changed files with 34 additions and 1 deletions
  1. +16
    -0
      README.md
  2. +12
    -0
      canonical_readlink.sh
  3. +6
    -1
      test/test.sh

+ 16
- 0
README.md View File

@@ -131,6 +131,22 @@ my_fun () {

```

## `canonical_readlink $filepath`

When you want to find out the directory the script you are currently running in, there are a number of UNIX filesystem indirections that are somewhat tricky to untangle. This function does that for you.

Signature:

```shell
canonical_readlink filepath<string>
```

Usage:

```shell
my_dir=`canonical_readlink "$0"`
```

## Debugging

Set `DEBUG=1` to get debugging output.

+ 12
- 0
canonical_readlink.sh View File

@@ -0,0 +1,12 @@
# Copyright (c) 2022 Jan Lehnardt <jan@apache.org>, MIT licensed
# via https://github.com/apache/couchdb/blob/1b8d4b73bc9ea67bfe0df5a41e24ea864f4c846d/rel/files/couchdb.in#L15 Apache 2 Licensed

canonical_readlink () {
file=$(dirname "$1")/$(basename "$1");
if [ -h "file" ]; then
cd $(dirname "$1")
canonical_readlink $(readlink "file");
else
cd "${1%/*}" && pwd -P;
fi
}

+ 6
- 1
test/test.sh View File

@@ -26,4 +26,9 @@
#
# echo test5
# (wait_for_pid_exit 45591 4 17)
# echo "test5 done"
# echo "test5 done"

# . canonical_readlink.sh
#
# my_path=`canonical_readlink "$0"`
# echo $my_path

Loading…
Cancel
Save