forked from oldratlee/useful-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rp
executable file
·39 lines (34 loc) · 895 Bytes
/
rp
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
32
33
34
35
36
37
38
39
#!/bin/bash
# @Function
# convert to Relative Path.
#
# @Usage
# # if 1 argument, print relative path to current dir.
# $ ./rp /etc/apache2/httpd.conf
# # if more than 1 argument, print relative path to last argument.
# $ ./rp a.txt ../b.txt /etc/passwd /etc/apache2
#
# @online-doc https://github.com/oldratlee/useful-scripts/blob/master/docs/shell.md#-ap-and-rp
# @author Jerry Lee (oldratlee at gmail dot com)
[ $# -eq 0 ] && {
echo "ERROR: NO argument!"
exit 1
}
[ $# -eq 1 ] && {
relativeTo=.
files=("$@")
} || {
argv=("$@")
argc=$#
# Get last argument
relativeTo="${argv[argc - 1]}"
files=( "${argv[@]:0:argc - 1}" )
}
[ -f "$relativeTo" ] && relativeTo="$(dirname "$relativeTo")"
for f in "${files[@]}" ; do
! [ -e $f ] && {
echo "$f does not exists!"
continue
}
realpath "$f" --relative-to="$relativeTo"
done