| @@ -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 | ## Debugging | ||||
| Set `DEBUG=1` to get debugging output. | Set `DEBUG=1` to get debugging output. | ||||
| @@ -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 | |||||
| } | |||||
| @@ -26,4 +26,9 @@ | |||||
| # | # | ||||
| # echo test5 | # echo test5 | ||||
| # (wait_for_pid_exit 45591 4 17) | # (wait_for_pid_exit 45591 4 17) | ||||
| # echo "test5 done" | |||||
| # echo "test5 done" | |||||
| # . canonical_readlink.sh | |||||
| # | |||||
| # my_path=`canonical_readlink "$0"` | |||||
| # echo $my_path | |||||