#!/bin/bash # go command function go() { ARG1=$1; ARG2=$2; ARG3=$3; if [[ -z "$ARG1" ]]; then __func_head "[MODE] SITE TARGET_DIR" __func_help " [MODE] TARGET_DIR" echo return 1 fi NAV=false; case "$ARG1" in p) CC='pushd' ;; c) CC='cd' ;; e) CC='echo' ;; up|down) NAV=true ;; *) CC='cd' ARG3=$ARG2 ARG2=$ARG1 ;; esac if [ $NAV = true ]; then __go_nav $ARG1 $ARG2 $ARG3 return 0; fi if [[ -f ~/.go_conf ]]; then source ~/.go_conf else echo "Cannot source config file" return 1; fi SITE=$(ls $UDEV|grep -m 1 $ARG2) if [[ -z "$SITE" ]]; then echo "No such site: $ARG2" ls $UDEV -1 | sed 's/^/ /g' return 1 fi if [[ -z "$ARG3" ]]; then $CC $UDEV/$SITE return 0 fi for LOC in ${LOCS[@]} do LOCATION=$(ls $UDEV/$SITE/$LOC/ 2> /dev/null | grep -m 1 $ARG3) if [[ -n "$LOCATION" ]]; then LOCATION="$LOC/$LOCATION" break; fi done if [[ -n "$LOCATION" ]]; then $CC $UDEV/$SITE/$LOCATION/ return 0 else echo "Location '$ARG3' not found under: $SITE" return 1 fi return 1 } __go_nav() { ARG1=$1; ARG2=$2; ARG3=$3; case "$ARG2" in p) CC='pushd' ;; c|"") CC='cd' ;; e) CC='echo' ;; *) CC='cd' ARG3=$ARG2 ;; esac if [[ "$ARG1" == "up" ]]; then IFS="/" read -a PWDA <<< "$(pwd)" MARK=0 LISTOF='' for i in "${!PWDA[@]}" do if [[ "${PWDA[$i]}" =~ $ARG3 ]]; then MARK=$i fi if [[ -n "${PWDA[$i]}" ]]; then LISTOF="$LISTOF\n ${PWDA[$i]}"; fi done if [ -z "$ARG3" ]; then MARK=$(( $MARK - 1 )) fi if [[ $MARK -eq 0 ]]; then echo "No such token in up stack: $ARG3" echo -e $LISTOF; echo return 1 fi DIR='/' for (( i=1; i<=$MARK; i++ )) do DIR="$DIR${PWDA[$i]}/" done else if [ -z "$ARG3" ]; then DIR=$(find . -maxdepth 1 -type d | head -n 2 | tail -n 1) else DIR=$(find . -type d -name '*'$ARG3'*') if [[ -z $DIR ]]; then echo "Directory not found: $ARG3" return 1 fi fi fi $CC $DIR }