-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathname.sh
More file actions
executable file
·28 lines (23 loc) · 874 Bytes
/
pathname.sh
File metadata and controls
executable file
·28 lines (23 loc) · 874 Bytes
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
#!/bin/sh
# Output the canonical path of a file given as argument.
# The argument can be any file (regular file, directory, symlink, fifo, etc.)
# The file given as argument may not exist; unlike other tools such as readlink
# or realpath, pathname.{pl,sh} never tries to resolve the path name given as
# argument, which can be absolute or relative.
# Usage:
# $ pathname.sh ../../..//foobar/.././etc///blah/../fstab//./blah/..
# /etc/fstab
set -eu
case "${1}" in
/*) pathname="${1}";;
*) pathname="${PWD}/${1}";;
esac
while true; do
case "${pathname}" in
*//*|*/./*|*/../*|*/|*/.|*/..)
pathname="$(echo "${pathname}" | sed -re 's,(/+\.?)+/+,/,g; s,^(/+\.\.)+(/+|$),/,; s,[^/]+/+\.\.(/+|$),/,; s,/+,/,g; s,(/+\.?)+$,,')" ;;
"") echo "/"; break ;;
*) echo "${pathname}"; break ;;
esac
done
# vim: et sts=4 sw=4 ts=4