Skip to content

Commit 13dd79b

Browse files
authored
Use a more portable method of detecting the current script's directory (#28)
The method used before relied on GNU readlink's -e CLI flag, which isn't supported on the version of readlink shipped with BSD and MacOs. Finding the current script's directory is tricky business. There are many StackOverflow questioins on this very topic, and many opinions on the "best" way to do it. The solution in this commit was carefully selected after reviewing many sources of information on the topic. Below are references to the most useful sources: * [Reliable way for a Bash script to get the full path to itself [duplicate]](https://stackoverflow.com/questions/4774054/reliable-way-for-a-bash-script-to-get-the-full-path-to-itself): * This is the best question on Stack Overflow because the question contains a specific set of requiremewnts (which are very close to our own requirements). In particular, [this answer](https://stackoverflow.com/a/9107028/106189) is helpful. * [Determining the Location of Your Script in Bash](https://www.binaryphile.com/bash/2020/01/12/determining-the-location-of-your-script-in-bash.html): * This is an excellent blog post which walks through and explains the solution in detail.
1 parent 79e5feb commit 13dd79b

9 files changed

+18
-9
lines changed

components/scripts/gradle/01-validate-incremental-building.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ readonly SHOW_RUN_ID=true
1515
# Needed to bootstrap the script
1616
SCRIPT_NAME=$(basename "$0")
1717
readonly SCRIPT_NAME
18-
SCRIPT_DIR="$(cd "$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")" && pwd)"
18+
# shellcheck disable=SC2164 # it is highly unlikely cd will fail here because we're cding to the location of this script
19+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"; cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo .)")"; pwd)"
1920
readonly SCRIPT_DIR
2021
readonly LIB_DIR="${SCRIPT_DIR}/lib"
2122

components/scripts/gradle/02-validate-local-build-caching-same-location.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ readonly SHOW_RUN_ID=true
1515
# Needed to bootstrap the script
1616
SCRIPT_NAME=$(basename "$0")
1717
readonly SCRIPT_NAME
18-
SCRIPT_DIR="$(cd "$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")" && pwd)"
18+
# shellcheck disable=SC2164 # it is highly unlikely cd will fail here because we're cding to the location of this script
19+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"; cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo .)")"; pwd)"
1920
readonly SCRIPT_DIR
2021
readonly LIB_DIR="${SCRIPT_DIR}/lib"
2122

components/scripts/gradle/03-validate-local-build-caching-different-locations.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ readonly SHOW_RUN_ID=true
1515
# Needed to bootstrap the script
1616
SCRIPT_NAME=$(basename "$0")
1717
readonly SCRIPT_NAME
18-
SCRIPT_DIR="$(cd "$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")" && pwd)"
18+
# shellcheck disable=SC2164 # it is highly unlikely cd will fail here because we're cding to the location of this script
19+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"; cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo .)")"; pwd)"
1920
readonly SCRIPT_DIR
2021
readonly LIB_DIR="${SCRIPT_DIR}/lib"
2122

components/scripts/gradle/04-validate-remote-build-caching-ci-ci.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ readonly SHOW_RUN_ID=false
1515
# Needed to bootstrap the script
1616
SCRIPT_NAME=$(basename "$0")
1717
readonly SCRIPT_NAME
18-
SCRIPT_DIR="$(cd "$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")" && pwd)"
18+
# shellcheck disable=SC2164 # it is highly unlikely cd will fail here because we're cding to the location of this script
19+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"; cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo .)")"; pwd)"
1920
readonly SCRIPT_DIR
2021
readonly LIB_DIR="${SCRIPT_DIR}/lib"
2122

components/scripts/gradle/05-validate-remote-build-caching-ci-local.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ readonly SHOW_RUN_ID=true
1515
# Needed to bootstrap the script
1616
SCRIPT_NAME=$(basename "$0")
1717
readonly SCRIPT_NAME
18-
SCRIPT_DIR="$(cd "$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")" && pwd)"
18+
# shellcheck disable=SC2164 # it is highly unlikely cd will fail here because we're cding to the location of this script
19+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"; cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo .)")"; pwd)"
1920
readonly SCRIPT_DIR
2021
readonly LIB_DIR="${SCRIPT_DIR}/lib"
2122

components/scripts/maven/01-validate-local-build-caching-same-location.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ readonly SHOW_RUN_ID=true
1515
# Needed to bootstrap the script
1616
SCRIPT_NAME=$(basename "$0")
1717
readonly SCRIPT_NAME
18-
SCRIPT_DIR="$(cd "$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")" && pwd)"
18+
# shellcheck disable=SC2164 # it is highly unlikely cd will fail here because we're cding to the location of this script
19+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"; cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo .)")"; pwd)"
1920
readonly SCRIPT_DIR
2021
readonly LIB_DIR="${SCRIPT_DIR}/lib"
2122

components/scripts/maven/02-validate-local-build-caching-different-locations.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ readonly SHOW_RUN_ID=true
1515
# Needed to bootstrap the script
1616
SCRIPT_NAME=$(basename "$0")
1717
readonly SCRIPT_NAME
18-
SCRIPT_DIR="$(cd "$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")" && pwd)"
18+
# shellcheck disable=SC2164 # it is highly unlikely cd will fail here because we're cding to the location of this script
19+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"; cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo .)")"; pwd)"
1920
readonly SCRIPT_DIR
2021
readonly LIB_DIR="${SCRIPT_DIR}/lib"
2122

components/scripts/maven/03-validate-remote-build-caching-ci-ci.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ readonly SHOW_RUN_ID=false
1515
# Needed to bootstrap the script
1616
SCRIPT_NAME=$(basename "$0")
1717
readonly SCRIPT_NAME
18-
SCRIPT_DIR="$(cd "$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")" && pwd)"
18+
# shellcheck disable=SC2164 # it is highly unlikely cd will fail here because we're cding to the location of this script
19+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"; cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo .)")"; pwd)"
1920
readonly SCRIPT_DIR
2021
readonly LIB_DIR="${SCRIPT_DIR}/lib"
2122

components/scripts/maven/04-validate-remote-build-caching-ci-local.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ readonly SHOW_RUN_ID=true
1515
# Needed to bootstrap the script
1616
SCRIPT_NAME=$(basename "$0")
1717
readonly SCRIPT_NAME
18-
SCRIPT_DIR="$(cd "$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")" && pwd)"
18+
# shellcheck disable=SC2164 # it is highly unlikely cd will fail here because we're cding to the location of this script
19+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"; cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo .)")"; pwd)"
1920
readonly SCRIPT_DIR
2021
readonly LIB_DIR="${SCRIPT_DIR}/lib"
2122

0 commit comments

Comments
 (0)