First release #1
77
rdlink.sh
77
rdlink.sh
@@ -16,12 +16,33 @@ set -o pipefail
|
|||||||
|
|
||||||
LOG_LEVEL="${LOG_LEVEL:-1}" # 7 = debug -> 0 = emergency
|
LOG_LEVEL="${LOG_LEVEL:-1}" # 7 = debug -> 0 = emergency
|
||||||
|
|
||||||
|
function rl::log () {
|
||||||
|
local log_level="${1:-${LOG_LEVEL}}"
|
||||||
|
shift
|
||||||
|
|
||||||
|
# all remaining arguments are to be printed
|
||||||
|
local log_line=""
|
||||||
|
|
||||||
|
while IFS=$'\n' read -r log_line; do
|
||||||
|
printf "%s [%9s] %s\n" "$(date -u +"%Y-%m-%d %H:%M:%S UTC")" "${log_level}" "${log_line}" 1>&2
|
||||||
|
done <<< "${@:-}"
|
||||||
|
}
|
||||||
|
emergency() { rl::log emergency "${@}"; exit 1; }
|
||||||
|
alarm() { [[ "${LOG_LEVEL:-0}" -ge 1 ]] && rl::log alert "${@}"; true; }
|
||||||
|
critical() { [[ "${LOG_LEVEL:-0}" -ge 2 ]] && rl::log critical "${@}"; true; }
|
||||||
|
error() { [[ "${LOG_LEVEL:-0}" -ge 3 ]] && rl::log error "${@}"; true; }
|
||||||
|
warning() { [[ "${LOG_LEVEL:-0}" -ge 4 ]] && rl::log warning "${@}"; true; }
|
||||||
|
notice() { [[ "${LOG_LEVEL:-0}" -ge 5 ]] && rl::log notice "${@}"; true; }
|
||||||
|
info() { [[ "${LOG_LEVEL:-0}" -ge 6 ]] && rl::log info "${@}"; true; }
|
||||||
|
debug() { [[ "${LOG_LEVEL:-0}" -ge 7 ]] && rl::log debug "${@}"; true; }
|
||||||
|
msg() { echo "$@"; true; }
|
||||||
|
|
||||||
|
### Script
|
||||||
rl::rdlink() {
|
rl::rdlink() {
|
||||||
local subject=
|
local subject=
|
||||||
local work=
|
local work=
|
||||||
local resolved=0
|
|
||||||
|
|
||||||
info "Processing: $@"
|
info "Processing: $*"
|
||||||
info " with pwd: $(pwd)"
|
info " with pwd: $(pwd)"
|
||||||
|
|
||||||
subject="$(rl::cleanpath "${1:-}")" || true
|
subject="$(rl::cleanpath "${1:-}")" || true
|
||||||
@@ -29,8 +50,8 @@ rl::rdlink() {
|
|||||||
# Follow multiple symlinks
|
# Follow multiple symlinks
|
||||||
while subject="$(rl::quicklink "${subject}")" ; do
|
while subject="$(rl::quicklink "${subject}")" ; do
|
||||||
# A link was resolved at least once
|
# A link was resolved at least once
|
||||||
|
:
|
||||||
info " rl::rdlink - Link found: $subject"
|
info " rl::rdlink - Link found: $subject"
|
||||||
resolved=1
|
|
||||||
done
|
done
|
||||||
|
|
||||||
# Special cases handling
|
# Special cases handling
|
||||||
@@ -47,19 +68,14 @@ rl::rdlink() {
|
|||||||
|
|
||||||
# Empty output if (dirname $subject) is not a valid path
|
# Empty output if (dirname $subject) is not a valid path
|
||||||
if ! work="$(rl::canon "${subject}")" ; then
|
if ! work="$(rl::canon "${subject}")" ; then
|
||||||
# Special: Links resolved to something like: /proc/2267178/fd/pipe:[22306727]
|
|
||||||
# are not existing files but `readlink -f` prints result anyway
|
|
||||||
# Printing result if a link was resolved at least once
|
|
||||||
#if (( ! resolved )); then
|
|
||||||
info " rl::rdlink exit - invalid path ${work}"
|
info " rl::rdlink exit - invalid path ${work}"
|
||||||
printf "\n"
|
printf "\n"
|
||||||
return 1
|
return 1
|
||||||
#fi
|
|
||||||
else
|
else
|
||||||
subject="${work}"
|
subject="${work}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
printf "${subject}\n"
|
printf "%s\n" "${subject}"
|
||||||
}
|
}
|
||||||
|
|
||||||
rl::quicklink() {
|
rl::quicklink() {
|
||||||
@@ -70,19 +86,19 @@ rl::quicklink() {
|
|||||||
|
|
||||||
# Check if current candidate is a symlink
|
# Check if current candidate is a symlink
|
||||||
if ! subject=$(readlink -- "${1:-}"); then
|
if ! subject=$(readlink -- "${1:-}"); then
|
||||||
printf -- "${1:-}\n"
|
printf -- "%s\n" "${1:-}"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
info " - symlink ${1} -> ${subject}"
|
info " rl::quicklink symlink ${1} -> ${subject}"
|
||||||
|
|
||||||
# relative symlink target; prepend its parent direcotry
|
# relative symlink target; prepend its parent direcotry
|
||||||
if [[ "${subject}" != "/"* ]]; then
|
if [[ "${subject}" != "/"* ]]; then
|
||||||
work="$(rl::canon "$(dirname -- "${1:-}")")"
|
work="$(rl::canon "$(dirname -- "${1:-}")")"
|
||||||
subject="${work}/${subject}"
|
subject="${work}/${subject}"
|
||||||
info " - relative link resolved: ${subject}"
|
info " rl::quicklink relative link resolved: ${subject}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
printf "${subject}\n"
|
printf "%s\n" "${subject}"
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,7 +156,7 @@ rl::canon() {
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
printf -- "${subject}\n"
|
printf -- "%s\n" "${subject}"
|
||||||
return ${retval}
|
return ${retval}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,7 +173,7 @@ rl::cleanpath() {
|
|||||||
done
|
done
|
||||||
|
|
||||||
info " rl::cleanpath result: ${work}"
|
info " rl::cleanpath result: ${work}"
|
||||||
printf -- "${work}\n"
|
printf -- "%s\n" "${work}"
|
||||||
}
|
}
|
||||||
|
|
||||||
rl::normalize() {
|
rl::normalize() {
|
||||||
@@ -188,7 +204,7 @@ rl::normalize() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
info " rl::normalize result: ${work}"
|
info " rl::normalize result: ${work}"
|
||||||
printf -- "${work}\n"
|
printf -- "%s\n" "${work}"
|
||||||
}
|
}
|
||||||
|
|
||||||
rl::main() {
|
rl::main() {
|
||||||
@@ -218,31 +234,9 @@ rl::main() {
|
|||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
function rl::log () {
|
|
||||||
local log_level="${1:-${LOG_LEVEL}}"
|
|
||||||
shift
|
|
||||||
|
|
||||||
# all remaining arguments are to be printed
|
|
||||||
local log_line=""
|
|
||||||
|
|
||||||
while IFS=$'\n' read -r log_line; do
|
|
||||||
printf "%s [%9s] %s\n" "$(date -u +"%Y-%m-%d %H:%M:%S UTC")" "${log_level}" "${log_line}" 1>&2
|
|
||||||
done <<< "${@:-}"
|
|
||||||
}
|
|
||||||
emergency() { rl::log emergency "${@}"; exit 1; }
|
|
||||||
alarm() { [[ "${LOG_LEVEL:-0}" -ge 1 ]] && rl::log alert "${@}"; true; }
|
|
||||||
critical() { [[ "${LOG_LEVEL:-0}" -ge 2 ]] && rl::log critical "${@}"; true; }
|
|
||||||
error() { [[ "${LOG_LEVEL:-0}" -ge 3 ]] && rl::log error "${@}"; true; }
|
|
||||||
warning() { [[ "${LOG_LEVEL:-0}" -ge 4 ]] && rl::log warning "${@}"; true; }
|
|
||||||
notice() { [[ "${LOG_LEVEL:-0}" -ge 5 ]] && rl::log notice "${@}"; true; }
|
|
||||||
info() { [[ "${LOG_LEVEL:-0}" -ge 6 ]] && rl::log info "${@}"; true; }
|
|
||||||
debug() { [[ "${LOG_LEVEL:-0}" -ge 7 ]] && rl::log debug "${@}"; true; }
|
|
||||||
msg() { echo "$@"; true; }
|
|
||||||
|
|
||||||
|
|
||||||
# Provide as function to be called when sourced
|
# Provide as function to be called when sourced
|
||||||
rdlink() {
|
rdlink() {
|
||||||
rl::rdlink "$@"
|
rl::main "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
### Check if script is _sourced
|
### Check if script is _sourced
|
||||||
@@ -251,7 +245,8 @@ _sourced=0
|
|||||||
if [ -n "${ZSH_EVAL_CONTEXT:-}" ]; then
|
if [ -n "${ZSH_EVAL_CONTEXT:-}" ]; then
|
||||||
case ${ZSH_EVAL_CONTEXT:-} in *:file) _sourced=1;; esac
|
case ${ZSH_EVAL_CONTEXT:-} in *:file) _sourced=1;; esac
|
||||||
elif [ -n "${KSH_VERSION:-}" ]; then
|
elif [ -n "${KSH_VERSION:-}" ]; then
|
||||||
[ "$(cd $(dirname -- $0) && pwd -P)/$(basename -- $0)" != "$(cd $(dirname -- ${.sh.file}) && pwd -P)/$(basename -- ${.sh.file})" ] && _sourced=1
|
[ "$(cd "$(dirname -- "$0")" && pwd -P)/$(basename -- "$0")" \
|
||||||
|
!= "$(cd "$(dirname -- "${.sh.file}")" && pwd -P)/$(basename -- "${.sh.file}")" ] && _sourced=1
|
||||||
elif [ -n "${BASH_VERSION:-}" ]; then
|
elif [ -n "${BASH_VERSION:-}" ]; then
|
||||||
(return 0 2>/dev/null) && _sourced=1
|
(return 0 2>/dev/null) && _sourced=1
|
||||||
else # All other shells: examine $0 for known shell binary filenames
|
else # All other shells: examine $0 for known shell binary filenames
|
||||||
@@ -264,3 +259,5 @@ if (( ! _sourced )); then
|
|||||||
rl::main "$@"
|
rl::main "$@"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
### Script EOF
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user