Thursday, November 29, 2012

sw ( switching directories on linux)

Sometimes i'm a little tired of cd-ing between directories.

A long time ago I had a sw function in korn shell made by Frans van der Meijs.

But now on linux we use bash.

After a small adjustment it runs on linux - bash as well, it can save time and typeing .

 Just make a file called .dests in your $HOME directory.

 put in some paths.
cat $HOME/.dests

/tmp
/home
$ORACLE_HOME
$ORACLE_HOME/rdbms/admin
$ORACLE_HOME/network/log
$TNS_ADMIN
$ORA_CRS_HOME

source this function

. $HOME/sw

Contents of the sw file :


DESTS_FILE=$HOME/.dests
sw()
{
  index=0
  while read LINE
  do
    if [ $? -ne 0 ]
    then break ;
    fi
    eval "MENU_ITEM[\${index}]=$LINE"
    index=`expr $index + 1 `
  done < $DESTS_FILE
  nr_of_dests=$index
  if [ $# -eq 1 ]
  then
    ANSWER=$1
  else
    echo ""
    echo "SW-INFO-01: Destinations"
    echo ""
    index=0
    while [ $index -lt $nr_of_dests ]
    do
      echo " --  ${index}   ${MENU_ITEM[${index}]} --"
      index=`expr $index + 1`
    done
    echo
    echo "Your choice [] "
    read ANSWER
  fi
  if test -d ${MENU_ITEM[$ANSWER]}
  then
    echo  "SW-INFO-02: Changing current working directory to ${MENU_ITEM[$ANSWER]} SUCCEEDED"
    cd ${MENU_ITEM[$ANSWER]}
  else
  echo  "SW-ERR-01 : Changing current working directory to ${MENU_ITEM[$ANSWER]} FAILED, DIRECTORY DOES NOT EXIST !"
  echo  "SW-INFO-03: please clean this entry from $HOME/.dests"
  fi
  pwd
}
filldest()
{
fc -l -10000 | awk '{print $2,$3}' | grep "cd \/" | sort | uniq | sed 's/cd //'>> $HOME/.dests
}
echo "SW-INFO-00: Use command >> sw << to switch directories ."
echo "SW-INFO-03: Use command >> filldest << to fill $DESTS_FILE ."


Usage :

sw 1
you switch to destination 1
sw 11
you switch to destination 11
sw
It will give you the menu.
Make a choice ....
HAPPY SWITCHING.

1 comment: