dm
changeset 0 32ec38f06438
child 1 9d965b78f426
equal deleted inserted replaced
-1:000000000000 0:32ec38f06438
       
     1 #!/bin/sh
       
     2 
       
     3 # dm 0.9 - Directory Manager
       
     4 #  (Instructions below the license)
       
     5 # LICENSE
       
     6 # Copyright (C) 2007 LluĂ­s Batlle i Rossell
       
     7 # 
       
     8 # This program is free software; you can redistribute it and/or
       
     9 # modify it under the terms of the GNU General Public License
       
    10 # as published by the Free Software Foundation; either version 2
       
    11 # of the License, or (at your option) any later version.
       
    12 # 
       
    13 # This program is distributed in the hope that it will be useful,
       
    14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    16 # GNU General Public License for more details.
       
    17 # 
       
    18 # You should have received a copy of the GNU General Public License
       
    19 # along with this program; if not, write to the Free Software
       
    20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
       
    21 #
       
    22 # Instructions:
       
    23 # 'dm' will manage a list of id-directory. 'dmcd' will allow the current shell to
       
    24 # change to the directory given by 'id'.
       
    25 #
       
    26 # Examples:
       
    27 #  $ dm 1 /tmp   # Adds the id '1' for /tmp
       
    28 #  $ dm 2 .      # Adds the id '2' for the current dir
       
    29 #  $ dmcd 1      # Goes to /tmp
       
    30 #  $ dmcd 2      # Goes to what was your current dir
       
    31 #  $ dm -d 2     # Deletes the entry for '2'.
       
    32 #  $ dm          # Shows your 'database'
       
    33 #  1     /tmp
       
    34 #  $ dm -h       # Shows help, and defines 'dmcd' for sh.
       
    35 #
       
    36 # In order to use 'dmcd', you should define the function
       
    37 # given by "dm -h" in your shell. Put it in your bashrc or
       
    38 # a file like that.
       
    39 #
       
    40 # Your directory database is stored in ~/.dm .
       
    41 
       
    42 
       
    43 function showdirs
       
    44 {
       
    45     if [ -f ~/.dm ] ; then
       
    46         cat ~/.dm
       
    47     fi
       
    48 }
       
    49 
       
    50 function testdir
       
    51 {
       
    52     if [ -f ~/.dm ] ; then
       
    53         grep "^$1	" ~/.dm > /dev/null 2> /dev/null
       
    54     else
       
    55         return 1
       
    56     fi
       
    57 }
       
    58 
       
    59 function deldir
       
    60 {
       
    61     # Remove the old def
       
    62     if [ -f ~/.dm ] ; then
       
    63         sed -i "/^$1\t/d" ~/.dm
       
    64     fi
       
    65 }
       
    66 
       
    67 function setdir
       
    68 {
       
    69     # Remove the old def
       
    70     deldir $1
       
    71     NEWDIR=`getpath "$2"`
       
    72     # Add new
       
    73     echo "$1	$NEWDIR" >> ~/.dm
       
    74 }
       
    75 
       
    76 function getdir
       
    77 {
       
    78     if [ -f ~/.dm ] ; then
       
    79         grep "^$1	" ~/.dm | sed 's/^[^\t]*\t//'
       
    80     else
       
    81         return 1
       
    82     fi
       
    83 }
       
    84 
       
    85 function showhelp
       
    86 {
       
    87     echo "usage: $0 [ -l | -d id | -t id | id | id newdir ]"
       
    88     echo 'For sh: function dmcd { dm -t $1 && cd `dm $1`; }'
       
    89 }
       
    90 
       
    91 function getpath
       
    92 {
       
    93     OLD=`pwd`
       
    94     cd "$1"
       
    95     pwd
       
    96     cd "$OLD"
       
    97 }
       
    98 
       
    99 # dm -t $1 && cd `dm $1`
       
   100 
       
   101 if [ $# -lt 1 ]; then
       
   102     showdirs
       
   103 elif [ $1 == "-l" ]; then
       
   104     showdirs
       
   105 elif [ $1 == "-h" ]; then
       
   106     showhelp
       
   107 elif [ $1 == "-d" ]; then
       
   108     deldir $2
       
   109 elif [ $1 == "-t" ]; then
       
   110     testdir $2
       
   111 elif [ $# -eq 2  ]; then
       
   112     setdir $1 $2
       
   113 elif [ $# -eq 1  ]; then
       
   114     getdir $1
       
   115 fi