blob: 55866683558032c00a36702e01a8b2108c66bdd4 (
plain) (
blame)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# Find an abbreviated path
ad() {
# Check argument count
if [ "$#" -ne 1 ] ; then
printf >&2 'ad(): Need just one argument\n'
return 2
fi
# Change the positional parameters from the abbreviated request
# to any matched directory
set -- "$(
# Clean up and anchor the request
req=${1%/}/
case $req in
(/*) ;;
(*) req=${PWD%/}/${req#/} ;;
esac
# Start building the target directory; go through the request piece by
# piece until it is used up
dir=
while [ -n "$req" ] ; do
# Chop the next front bit off the request and add it to the dir
dir=${dir%/}/${req%%/*}
req=${req#*/}
# If that exists, all is well and we can keep iterating
[ -d "$dir" ] && continue
# Set the positional parameters to a glob expansion of the
# abbreviated directory given
set -- "$dir"*
# Iterate through the positional parameters filtering out
# directories; we need to run right through the whole list to check
# that we have at most one match
entd=
for ent ; do
[ -d "$ent" ] || continue
# If we already found a match and have found another one, bail
# out
if [ -n "$entd" ] ; then
printf >&2 'ad(): More than one matching dir for %s*:\n' \
"$dir"
printf >&2 '%s\n' "$@"
exit 1
fi
# Otherwise, this can be our first one
entd=$ent
done
# If we found no match, bail out
if [ -z "$entd" ] ; then
printf >&2 'ad(): No matching dirs: %s*\n' "$dir"
exit 1
fi
# All is well, tack on what we have found and keep going
dir=$entd
done
# Print the target with trailing slash to work around newline stripping
printf '%s/' "${dir%/}"
)"
# Remove trailing slash
set -- "${1%/}"
# If the subshell printed nothing, return with failure
[ -n "$1" ] || return
# Try to change into the determined directory
command cd -- "$@"
}
|