-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjumpdir.sh
More file actions
executable file
·127 lines (105 loc) · 1.98 KB
/
jumpdir.sh
File metadata and controls
executable file
·127 lines (105 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/bash
# Author: Siyuan Liu
# E-mail: siyuanl96@gmail.com
# ALIAS_DB="$(cd $(dirname ${BASH_SOURCE[0]}); pwd)/.jumpdir.d"
ALIAS_DB="/home/`whoami`/.jumpdir.d/"
ALIAS_CMDLIST="${ALIAS_DB}/cmdlist"
ALIAS_COMP="${SHELL_CMD_COMP_DIR}/jcd.comp"
if [ ! -d "$ALIAS_DB" ]; then
mkdir $ALIAS_DB
fi
if [ ! -f $ALIAS_COMP ]; then
touch $ALIAS_COMP
echo "jcd" > $ALIAS_COMP
echo " @" >> $ALIAS_COMP
fi
if [ ! -f "$ALIAS_CMDLIST" ]; then
touch $ALIAS_CMDLIST
fi
Usage() {
echo -e " -a Add or modify an alias."
echo -e " -d Delete an alias."
echo -e " -t Echo target directory."
echo -e " -l List all aliases."
}
ParseOpts() {
while getopts ":a:d:t:l" opt
do
case $opt in
a)
arr=(`echo $OPTARG | tr '=' ' '`)
alias=${arr[0]}
path=${arr[1]}
SaveAlias $alias $path
;;
d)
DelAlias $OPTARG
;;
t)
JumpTo $OPTARG
;;
l)
ListAlias
;;
*)
Usage
exit 1
;;
esac
done
}
ListAlias() {
tmp=1
cat $ALIAS_CMDLIST | while read line
do
if [ $tmp == 1 ]; then
echo -e "\e[32;40m$line\E[0m"
tmp=0
else
echo -e "\e[36;40m$line\E[0m"
tmp=1
fi
done
}
DelAlias() {
sed -i '/^'$1'=/d' $ALIAS_CMDLIST
sed -i '/^\t'$1'$/d' $ALIAS_COMP
}
SaveAlias() {
path=$(SearchAlias $1)
if [ "$path" != "" ]; then
echo -e "The alias \e[32;40m$1=$path\E[0m already exist. Do you want to replace it? (y/n)"
read continue
if [[ "$continue" != "y" ]]; then
exit 0
fi
sed -i '/^'$1'=/d' $ALIAS_CMDLIST
fi
sed -i '/^\t'$1'$/d' $ALIAS_COMP
echo " $1" >> $ALIAS_COMP
echo "$1=$2" >> ${ALIAS_CMDLIST}
sort ${ALIAS_CMDLIST} -o ${ALIAS_CMDLIST}
}
# return path
SearchAlias() {
alias=$1
path=`awk -F = '/^'$alias'=.+/ {print $2}' ${ALIAS_CMDLIST}`
echo $path
}
JumpTo() {
dst=$(SearchAlias $1)
if [ "$dst" == "" ]; then
echo "Alias does not exist."
exit 1
fi
if [ ! -d $dst ] || [ "$dst" == "" ]; then
echo -e "The directory \033[31m$dst\E[0m does not exist."
exit 1
fi
echo $dst
}
if (( $# < 1 )); then
Usage
exit 1
fi
ParseOpts $@