@@ -14,43 +14,22 @@ set -eEuo pipefail
1414# util functions
1515# ###############################################################################
1616
17- # How can I get the behavior of GNU's readlink -f on a Mac?
18- # https://stackoverflow.com/questions/1055671
19- portableReadLink () {
20- local file=$1 uname
21-
22- uname=$( uname)
23- case " $uname " in
24- Linux* | CYGWIN* | MINGW* )
25- readlink -f -- " $file "
26- ;;
27- Darwin* )
28- local py_args=(-c ' import os, sys; print(os.path.realpath(sys.argv[1]))' " $file " )
29- if command -v greadlink > /dev/null; then
30- greadlink -f -- " $file "
31- elif command -v python3 > /dev/null; then
32- python3 " ${py_args[@]} "
33- elif command -v python > /dev/null; then
34- python " ${py_args[@]} "
35- else
36- echo " fail to find command(greadlink/python3/python) for readlink!" >&2
37- exit 1
38- fi
39- ;;
40- * )
41- echo " not support uname($uname )!" >&2
42- exit 1
43- ;;
44- esac
17+ # `realpath` command existed on Linux and macOS, return resolved physical path
18+ # - realpath command on macOS do NOT support option `-e`;
19+ # combined `[ -e $file ]` to check file existence first.
20+ # - How can I get the behavior of GNU's readlink -f on a Mac?
21+ # https://stackoverflow.com/questions/1055671
22+ realpath () {
23+ [ -e " $1 " ] && command realpath -- " $1 "
4524}
4625
4726# ###############################################################################
4827# biz logic
4928# ###############################################################################
5029
5130# DO NOT inline THIS_SCRIPT into BASE_DIR, because sub-shell:
52- # BASE_DIR=$(dirname -- "$(portableReadLink "${BASH_SOURCE[0]}")")
53- THIS_SCRIPT=$( portableReadLink " ${BASH_SOURCE[0]} " )
31+ # BASE_DIR=$(dirname -- "$(realpath "${BASH_SOURCE[0]}")")
32+ THIS_SCRIPT=$( realpath " ${BASH_SOURCE[0]} " )
5433BASE_DIR=$( dirname -- " $THIS_SCRIPT " )
5534
5635# shellcheck disable=SC1091
0 commit comments