author | viric@llimona |
Mon, 14 May 2007 10:04:57 +0200 | |
changeset 3 | 5b5abb696426 |
parent 2 | 11c4ec7423de |
permissions | -rwxr-xr-x |
0 | 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: |
|
2
11c4ec7423de
'sort' sorts not-numerically, and fixed text width to 80 cols.
viric@llimona
parents:
1
diff
changeset
|
23 |
# 'dm' will manage a list of id-directory. 'dmcd' will allow the current shell |
11c4ec7423de
'sort' sorts not-numerically, and fixed text width to 80 cols.
viric@llimona
parents:
1
diff
changeset
|
24 |
# to change to the directory given by 'id'. |
0 | 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 |
function showdirs |
|
43 |
{ |
|
44 |
if [ -f ~/.dm ] ; then |
|
2
11c4ec7423de
'sort' sorts not-numerically, and fixed text width to 80 cols.
viric@llimona
parents:
1
diff
changeset
|
45 |
cat ~/.dm | sort |
0 | 46 |
fi |
47 |
} |
|
48 |
||
49 |
function testdir |
|
50 |
{ |
|
3 | 51 |
if [ "$1x" == "x" ]; then |
52 |
echo "Error: You should specify an alias for a directory." 1>&2 |
|
53 |
return 1 |
|
54 |
fi |
|
0 | 55 |
if [ -f ~/.dm ] ; then |
56 |
grep "^$1 " ~/.dm > /dev/null 2> /dev/null |
|
3 | 57 |
if [ $? -ne 0 ]; then |
58 |
echo "Error: There is no directory for '$1'" 1>&2 |
|
59 |
return 1 |
|
60 |
fi |
|
0 | 61 |
else |
3 | 62 |
echo "Error: There is no directory for '$1'" 1>&2 |
0 | 63 |
return 1 |
64 |
fi |
|
65 |
} |
|
66 |
||
67 |
function deldir |
|
68 |
{ |
|
69 |
# Remove the old def |
|
70 |
if [ -f ~/.dm ] ; then |
|
71 |
sed -i "/^$1\t/d" ~/.dm |
|
72 |
fi |
|
73 |
} |
|
74 |
||
75 |
function setdir |
|
76 |
{ |
|
77 |
# Remove the old def |
|
78 |
deldir $1 |
|
79 |
NEWDIR=`getpath "$2"` |
|
80 |
# Add new |
|
81 |
echo "$1 $NEWDIR" >> ~/.dm |
|
82 |
} |
|
83 |
||
84 |
function getdir |
|
85 |
{ |
|
86 |
if [ -f ~/.dm ] ; then |
|
87 |
grep "^$1 " ~/.dm | sed 's/^[^\t]*\t//' |
|
88 |
else |
|
89 |
return 1 |
|
90 |
fi |
|
91 |
} |
|
92 |
||
93 |
function showhelp |
|
94 |
{ |
|
95 |
echo "usage: $0 [ -l | -d id | -t id | id | id newdir ]" |
|
96 |
echo 'For sh: function dmcd { dm -t $1 && cd `dm $1`; }' |
|
97 |
} |
|
98 |
||
99 |
function getpath |
|
100 |
{ |
|
101 |
OLD=`pwd` |
|
102 |
cd "$1" |
|
103 |
pwd |
|
104 |
cd "$OLD" |
|
105 |
} |
|
106 |
||
107 |
# dm -t $1 && cd `dm $1` |
|
108 |
||
109 |
if [ $# -lt 1 ]; then |
|
110 |
showdirs |
|
111 |
elif [ $1 == "-l" ]; then |
|
112 |
showdirs |
|
113 |
elif [ $1 == "-h" ]; then |
|
114 |
showhelp |
|
115 |
elif [ $1 == "-d" ]; then |
|
116 |
deldir $2 |
|
117 |
elif [ $1 == "-t" ]; then |
|
118 |
testdir $2 |
|
119 |
elif [ $# -eq 2 ]; then |
|
120 |
setdir $1 $2 |
|
121 |
elif [ $# -eq 1 ]; then |
|
122 |
getdir $1 |
|
123 |
fi |