-
Notifications
You must be signed in to change notification settings - Fork 5
/
path
executable file
·31 lines (26 loc) · 838 Bytes
/
path
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/bash
# get a file's absolute path, function similar with linux command "readlink -f $filename"
# usage: path $filename
# source from: https://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
# created by zhouwei on 2017-10-10
if [[ $# < 1 ]]; then
echo "Usage: path \$filename"
exit 1
fi
set -e
TARGET_FILE=$1
ls $TARGET_FILE > /dev/null
cd `dirname $TARGET_FILE`
TARGET_FILE=`basename $TARGET_FILE`
# Iterate down a (possible) chain of symlinks
while [ -L "$TARGET_FILE" ]
do
TARGET_FILE=`readlink $TARGET_FILE`
cd `dirname $TARGET_FILE`
TARGET_FILE=`basename $TARGET_FILE`
done
# Compute the canonicalized name by finding the physical path
# for the directory we're in and appending the target file.
PHYS_DIR=`pwd -P`
RESULT=$PHYS_DIR/$TARGET_FILE
echo $RESULT