LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-17-2011, 11:36 AM   #1
JurgyMan
Member
 
Registered: Jan 2004
Location: USA
Distribution: Gentoo
Posts: 60

Rep: Reputation: 15
go: a cd/pushd/popd/dirs replacement


I have to use the older ksh on SUN and AIX alot...
and kinda missed the pushd concept for jumping around a bunch of directories.
but not really, because I didnt always want to pop off my directory, just wanted to cd there.

So I wrote a ksh or bash friendly script with usage as below.
Make it available by sourcing: . godir.sh

Code:
# Script:  godir
# Written: 05-16-11 13:49:17 By: AWJurgensen
# Implements "go" utility. go to a directory maintained from an array
# go is a cd/pushd/popd replacement, that saves your paths for future go's
# environment variable GODIR contains all directory entries
# EDIT flds array definition below for your shell:  ksh or bash
# LOAD it up by sourcing into your shell:   . godir.sh

GOUSAGE='Example usage of the go command (a cd/pushd/popd/dirs alternative):
  go          list the GODIR directory array
  go 2        goto directory entry 2
  go -3       remove directory entry 3
  go -!       remove all directory entries
  go -?       show this usage help
  go newdir   cd to newdir, creating new entry
  go 9:newdir cd to newdir, creating/updating entry 9
  go w:/work  cd to work, creating/updating entry w'


# this is the array in var=val:var=val format
#GODIR=0=/full/path:1=/path
typeset GODIR
export GODIR

# Usage: Array {add|del|get|dump|exists|arrayadd} [pair]
# pair is in format: var=value
function GoArray {
  #set -x
  typeset cmd=${1} pair=${2}
  typeset -i add=0 delete=0 get=0 dump=0 debug=0
  case $cmd in
    add)        add=1 ;;
    del*)       delete=1 ;;
    get)        get=1 ;;
    dump)       dump=1 ;;
    exists)     if [ "$(echo "$GODIR" |grep ${pair}=)" != "" ] ; then
                  return 0
                else
                  return 1
                fi ;;
    arrayadd)   if [ "$GODIR" != "" ] ; then
                  GODIR="$GODIR:$pair"
                else
                  GODIR="$pair"
                fi
                return ;;
    *)          echo "Array: Unsupported action $cmd"
                return ;;
  esac
  typeset OIFS IFS field var value flds
  typeset -i err=0 i=0 numflds=0

  ((debug)) && print -u2 "pair: $pair"
  OIFS=$IFS
  IFS=:
  set -A flds -- ${GODIR}       # ksh only;  load array flds
  # flds=(${GODIR})             # bash only; load array flds
  IFS=$OIFS
  numflds=${#flds[*]}
  if [[ add -gt 0 ]] ; then
    add=numflds
    GoArray arrayadd "$add=$pair"
    return
  fi
  GODIR=""
  # loop thru all N=/path entries:
  while [[ i -lt numflds ]]; do
    field=${flds[i]}
    IFS==
    set -- $field
    IFS=$OIFS
    var=${1}
    value=${2}
    if [[ delete -gt 0 && $var = "$pair" ]] ; then
      ((debug)) && print -u2 "deleting: $pair"
    elif [[ get -gt 0 && $var = "$pair" ]] ; then
      echo "$value"
      GoArray arrayadd "$field"
    elif [[ dump -gt 0 ]] ; then
      printf "%-2.2s %s\n" $var "$value"
      GoArray arrayadd "$field"
    # Add is handled before the loop
    #elif [[ add -gt 0 -a add -eq i ] ; then
    #  GoArray arrayadd "$add=$pair"
    else
      GoArray arrayadd "$field"
    fi
    ((i+=1))
  done
}

# go is a cd replacement, that stores your directories for future go's
function go
{
  NUL=/dev/null
  #set -x
  case "$1" in
    -!)  GODIR="" ;;
    -\?) echo "$GOUSAGE" ;;
    -*)  GoArray del ${1#-}
         GoArray dump | sort ;;
    *:*)  lbl=${1%:*} # remove pattern on right
          dir=${1#*:} # remove pattern on left
          if [ -d "$dir" ] ; then
           cd "$dir" >$NUL ; echo go: $(/bin/pwd)
           # Check if were replacing an entry. then do delete, add:
           GoArray exists ${lbl} && GoArray del ${lbl}
           GoArray arrayadd "${lbl}=$(/bin/pwd)"
         else
           GoArray dump | sort
         fi ;;
    *)   if [ -d "$1" ] ; then
           cd "$1" >$NUL ; echo go: $(/bin/pwd)
           GoArray add "$(/bin/pwd)"
         else
           GO=$(GoArray get "$1")
           if [ -d "$GO" ] ; then
             cd "$GO" >$NUL ; echo go: $(/bin/pwd)
           else
             GoArray dump | sort
           fi
         fi ;;
  esac
}
Here's what I added to .profile to "seed" GODIR with my favorite directories:
Code:
# load go function (a cd alternative)
. $HOME/bin/godir.sh
# load up our favorite directories for use by: go
GODIR='0=/home/alan:b=/home/alan/bin:d=/home/alan/doc:s=/home/alan/shells:p=/home/alan/perl:w=/work:m=/data/media/mp3:l=/var/log:t=/tmp'
alias g='cd' # go home
alias b='cd -' # go back to previous dir

Last edited by JurgyMan; 05-18-2011 at 11:18 AM. Reason: updated the script code
 
Old 05-18-2011, 03:06 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Thanks for sharing that. Hoping you'll fix the documented bug and post the corrected version here.
A small picky comment: your shebang is incorrect, first it should refer to /bin/ksh or bash or xpg4/sh as your syntax isn't compatible with Solaris /bin/sh and anyway it shouldn't probably be there in the first place as you are defining a couple of functions and variables in a file intended to be sourced, not executed.
 
Old 03-05-2023, 01:53 AM   #3
jgmm
LQ Newbie
 
Registered: Mar 2023
Posts: 1

Rep: Reputation: 0
Thanks

this is so cool.
thanks.
 
Old 03-05-2023, 02:08 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
These days, I'd recommended changing the name of the command to something other than "go".
 
1 members found this post helpful.
Old 03-07-2023, 10:54 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,793

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Some simplifications can be done. An external command like grep is to be avoided.
Code:
    exists)     if [ "$(echo "$GODIR" |grep ${pair}=)" != "" ] ; then
should become
Code:
    exists)     if [[ :"$GODIR": == *:${pair}=* ]] ; then
where I have added a left : boundary.
Note the == takes a shell glob not an RE like grep. If a glob is not wanted at all then put quotes: *:"${pair}="*

Last edited by MadeInGermany; 03-07-2023 at 11:14 AM.
 
  


Reply

Tags
ksh, script



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] tcsh: pushd + popd in one function lucmove Programming 1 07-03-2010 06:56 PM
pushd &popd itsmesee Linux - Newbie 8 12-02-2007 11:10 PM
pushd popd dirs christianunix Linux - Newbie 1 10-26-2007 10:10 PM
puhsd, popd implementation Rudess Linux - General 1 03-12-2007 06:37 PM
How to see just dirs with ls? d1s4st3r Linux - Newbie 3 06-04-2004 10:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration