Skip to content

Commit a3b7e7a

Browse files
committed
Added fallbacks to flux.sh in case realpath is not available.
The bash script now uses $BASH_SOURCE to find out its name. If `realpath` is not available `readlink` is tried. If this is not available the script attempts to extract link targets from ls output. Link resolution is now only attempted if $BASH_SOURCE contains a soft link.
1 parent c4782ee commit a3b7e7a

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/main/scripts/flux.sh

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,39 @@
11
#!/bin/bash
22

3-
METAFACTURE_HOME=$( dirname "$( realpath "$0" )" )
3+
# `cd` is used with a relative path in this script. We may
4+
# end in an unexpected location if CDPATH is not empty.
5+
unset CDPATH
6+
7+
if [ -z "$BASH_SOURCE" ] ; then
8+
echo "Error: cannot determine script location (\$BASH_SOURCE is not set)" >&2
9+
exit 2
10+
fi
11+
# If a symbolic link is used to invoke this script this link
12+
# needs to be resolved in order to find the directory where
13+
# the actual script file lives. Several tools exist for
14+
# resolving symbolic links. However, none of these tools is
15+
# available on all platforms. Hence, we try different ones
16+
# until we succeed.
17+
if [ -L "$BASH_SOURCE" ] ; then
18+
script_file=$( realpath "$BASH_SOURCE" 2> /dev/null ) ||
19+
script_file=$( readlink -f "$BASH_SOURCE" 2> /dev/null ) ||
20+
script_file=$(
21+
file=$BASH_SOURCE
22+
while [ -L "$file" ] ; do
23+
cd "$( dirname "$file" )"
24+
link_name=$( basename "$file" )
25+
file=$( readlink "$link_name" 2> /dev/null ) ||
26+
file=$( ls -ld "$link_name" | sed "s/^.\+ -> \(.\+\)$/\1/g" )
27+
done
28+
cd "$( dirname "$file" )"
29+
echo "$( pwd )/$( basename "$file" )"
30+
)
31+
else
32+
script_file=$BASH_SOURCE
33+
fi
34+
# Remove script file name from the path and make sure
35+
# that the path is absolute:
36+
METAFACTURE_HOME=$( cd "$( dirname "$script_file" )" ; pwd )
437

538
# Fix path if running under cygwin:
639
if uname | grep -iq cygwin ; then

0 commit comments

Comments
 (0)