-
Notifications
You must be signed in to change notification settings - Fork 1
/
cd-dot-dot.bash
71 lines (54 loc) · 1.18 KB
/
cd-dot-dot.bash
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
# Copyright (c) 2023 Michael Federczuk
# SPDX-License-Identifier: MPL-2.0 AND Apache-2.0
function cd..() {
#region args
local -i stepc || return
case $# in
(0)
stepc=1 || return
;;
(1)
if [ -z "$1" ]; then
printf '%s: argument must not be empty\n' "${FUNCNAME[0]}" >&2
return 9
fi
if [[ ! "$1" =~ ^['+-']?['0'-'9']+$ ]]; then
printf '%s: %s: not an integer\n' "${FUNCNAME[0]}" "$1" >&2
return 10
fi
stepc=$(("$1")) || return
if ((stepc < 0)); then
printf '%s: %s: out of range (< 0)\n' "${FUNCNAME[0]}" "$1" >&2
return 11
fi
;;
(*)
{
printf '%s: too many arguments: %i\n' "${FUNCNAME[0]}" $#
printf 'usage: %s [<step count>]\n' "${FUNCNAME[0]}"
} >&2
return 4
;;
esac
readonly stepc || return
#endregion
if ((stepc == 0)); then
# no-op
return
fi
#region building pathname
local pathname || return
local -i i || return
for ((i = 0; i < stepc; ++i)); do
if [ -n "$pathname" ]; then
pathname+='/' || return
fi
pathname+='..' || return
done
unset -v i || return
readonly pathname || return
#endregion
# shellcheck disable=2164
cd -- "$pathname"
}
complete cd.. # disables completion