1 | #!/bin/bash |
---|
2 | # Script to replace uses of 'realpath' and/or 'command -v' |
---|
3 | |
---|
4 | echoerr() { |
---|
5 | cat <<< "$@" 1>&2 |
---|
6 | } |
---|
7 | |
---|
8 | arb_realpath() { |
---|
9 | # replacement for 'realpath' |
---|
10 | # |
---|
11 | # Note: this function is duplicated in ../arb_install.sh@arb_realpath |
---|
12 | local FILE="$1"; shift |
---|
13 | |
---|
14 | local REALPATH=$(realpath "$FILE" 2>/dev/null ) |
---|
15 | local READLINK=$(readlink -f "$FILE" 2>/dev/null ) |
---|
16 | |
---|
17 | if [ -z "${REALPATH}" ]; then |
---|
18 | if [ -z "${READLINK}" ]; then |
---|
19 | echoerr "Warning: neighter realpath nor readlink works here" |
---|
20 | if [ -e "${FILE}" ]; then |
---|
21 | local PWDFILE="$(pwd)/${FILE}" |
---|
22 | if [ -e "${PWDFILE}" ]; then echo "${PWDFILE}" |
---|
23 | else echo "${FILE}" |
---|
24 | fi |
---|
25 | else echo "${FILE}" |
---|
26 | fi |
---|
27 | else echo "${READLINK}" |
---|
28 | fi |
---|
29 | else |
---|
30 | if [ -z "${READLINK}" ]; then echo "${REALPATH}" |
---|
31 | else |
---|
32 | if [ "${READLINK}" != "${REALPATH}" ]; then |
---|
33 | echoerr "Warning: realpath and readlink differ (REALPATH=${REALPATH} READLINK=${READLINK})" |
---|
34 | fi |
---|
35 | echo "${REALPATH}" |
---|
36 | fi |
---|
37 | fi |
---|
38 | } |
---|
39 | |
---|
40 | arb_full_cmd() { |
---|
41 | # report full path of external command (as found by shell via PATH) |
---|
42 | local CMD="$1"; shift |
---|
43 | local FULLCMD=$(command -v "${CMD}" 2>/dev/null ) |
---|
44 | if [ -n "${FULLCMD}" -a ! -x "${FULLCMD}" ]; then echoerr "[no valid full command for '${CMD}: '${FULLCMD}']"; FULLCMD=; fi |
---|
45 | if [ -z "${FULLCMD}" ]; then |
---|
46 | FULLCMD=$(hash -t "${CMD}" 2>/dev/null ) |
---|
47 | if [ -n "${FULLCMD}" -a ! -x "${FULLCMD}" ]; then echoerr "[no valid full command for '${CMD}: '${FULLCMD}']"; FULLCMD=; fi |
---|
48 | if [ -z "${FULLCMD}" ]; then |
---|
49 | FULLCMD=$(which "${CMD}" 2>/dev/null ) |
---|
50 | if [ -n "${FULLCMD}" -a ! -x "${FULLCMD}" ]; then echoerr "[no valid full command for '${CMD}: '${FULLCMD}']"; FULLCMD=; fi |
---|
51 | fi |
---|
52 | fi |
---|
53 | if [ -n "${FULLCMD}" ]; then |
---|
54 | FULLCMD=$(arb_realpath "${FULLCMD}") |
---|
55 | fi |
---|
56 | echo "${FULLCMD}" |
---|
57 | } |
---|
58 | |
---|
59 | usage_error() { |
---|
60 | local ERR="$1"; shift |
---|
61 | |
---|
62 | echoerr "Usage: arb_path.sh -r file" |
---|
63 | echoerr " replacement for 'realpath file' (generates canonical pathname)." |
---|
64 | echoerr " falls back to given input on failure to canonicalize." |
---|
65 | echoerr "Usage: arb_path.sh -x cmd" |
---|
66 | echoerr " replacement for 'command -v file' (finds executable; absolute, relative or via PATH)." |
---|
67 | echoerr " returns empty string if 'cmd' not found." |
---|
68 | echoerr "" |
---|
69 | echoerr "Error: ${ERR}" |
---|
70 | exit 1 |
---|
71 | } |
---|
72 | |
---|
73 | FLAG="$1" ; shift |
---|
74 | ARG="$1" ; shift |
---|
75 | |
---|
76 | if [ -z "${ARG}" ]; then |
---|
77 | usage_error "missing argument(s)" |
---|
78 | else |
---|
79 | if [ "${FLAG}" = "-r" ]; then |
---|
80 | echo $(arb_realpath "${ARG}") |
---|
81 | else |
---|
82 | if [ "${FLAG}" = "-x" ]; then |
---|
83 | echo $(arb_full_cmd "${ARG}") |
---|
84 | else |
---|
85 | usage_error "Unknown flag '${FLAG}'" |
---|
86 | fi |
---|
87 | fi |
---|
88 | fi |
---|
89 | |
---|
90 | |
---|