blob: a399770291f31c5863ea59a4f906423161abd82b (
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
|
# Shortcut to step up the directory tree with an arbitrary number of steps,
# like cd .., cd ../.., etc
ud() {
# Check arguments; default to 1 and $PWD
if [ "$#" -gt 2 ] ; then
printf >&2 'ud(): Too many arguments\n'
return 2
fi
set -- "${1:-1}" "${2:-"$PWD"}"
set -- "$1" "$2"
# Check first argument, number of steps upward. "0" is weird, but valid;
# "-1" however makes no sense at all
if [ "$1" -lt 0 ] ; then
printf >&2 'ud(): Invalid step count\n'
return 2
fi
# Check second argument, starting path, for relativity and anchor it if
# need be
case $2 in
/*) ;;
*) set -- "$1" "$PWD"/"$2" ;;
esac
# Chop an element off the target the specified number of times
while [ "$1" -gt 0 ] ; do
# Make certain there are no trailing slashes to foul us up
while : ; do
case $2 in
*/) set -- "$1" "${2%/}" ;;
*) break ;;
esac
done
# Strip a path element
set -- "$(($1-1))" "${2%/*}"
done
# Shift off the count, which should now be zero
shift
# Try to change into the determined directory, or the root if blank
command cd -- "${1:-/}"
}
|