File: bd

package info (click to toggle)
bd 1.02-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 276 kB
  • sloc: sh: 110; makefile: 2
file content (55 lines) | stat: -rwxr-xr-x 1,014 bytes parent folder | download | duplicates (4)
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
#! /bin/bash
usage_error () {
  cat << EOF
------------------------------------------------------------------
Name: bd
Version: 1.02

------------------------------------------------------------------
Description: Go back to a specified directory up in the hierarchy.

------------------------------------------------------------------
How to use:

Please refer https://github.com/vigneshwaranr/bd

EOF
}

newpwd() {
  oldpwd=$1
  case "$2" in
    -s)
      pattern=$3
      NEWPWD=$(echo $oldpwd | sed 's|\(.*/'$pattern'[^/]*/\).*|\1|')
      ;;
    -si)
      pattern=$3
      NEWPWD=$(echo $oldpwd | perl -pe 's|(.*/'$pattern'[^/]*/).*|$1|i')
      ;;
    *)
      pattern=$2
      NEWPWD=$(echo $oldpwd | sed 's|\(.*/'$pattern'/\).*|\1|')
  esac
}

if [ $# -eq 0 ]
then
  usage_error
elif [ "${@: -1}" = -v ]
then
  usage_error
else
  oldpwd=$(pwd)

  newpwd "$oldpwd" "$@"
  
  if [ "$NEWPWD" = "$oldpwd" ]
  then
    echo "No such occurrence."
  else
    echo $NEWPWD
    cd "$NEWPWD"
  fi
  unset NEWPWD
fi