-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadmove.bash
120 lines (107 loc) · 2.98 KB
/
loadmove.bash
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
# Copyright [2013-2016] [Gregory Senay]
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#!/bin/bash
# call this script by using source
# source loadmove.bash
export DIRLOADMOVE=~/.dir_tmp
if [ ! -d $DIRLOADMOVE ] ; then
mkdir $DIRLOADMOVE
fi
function help {
echo "load move - (C) 2013-2016 Gregory Senay."
echo "Released under the GNU GPL."
echo ""
echo "usage:"
echo ""
echo "load <alias> \t To save path of the directory"
echo "\t\t Then change directory, go anywhere"
echo "move <alias>\t To return the previous saved directory <alias>"
echo ""
echo "load -r <alias> \t To delete the saved directory alias"
echo "load -i <alias> \t Show the path of the saved directory alias"
echo ""
echo "\t\t<alias> can be any string"
}
function list_all(){ # get the list of all saved directory
echo `ls -a ~/.dir_tmp/|grep ".load_directory"|cut -d"." -f3`;
}
function load {
if [[ "$#" == "2" && "$1" == "-r" ]] ; then
if [ -f $DIRLOADMOVE/.load_directory.$2 ] ; then
#echo "delete $2 $DIRLOADMOVE/.load_directory.$2"
rm $DIRLOADMOVE/.load_directory.$2
return
else
echo "No preload directory $2"
return
fi
fi
if [[ "$#" == "2" && "$1" == "-i" ]] ; then
if [ -f $DIRLOADMOVE/.load_directory.$2 ] ; then
#echo "delete $2 $DIRLOADMOVE/.load_directory.$2"
cat $DIRLOADMOVE/.load_directory.$2
return
else
echo "No preload directory $2"
return
fi
fi
if [ "$#" -gt 1 ] ; then
help
return
else
for var in "$@" ; do
if [[ $var == "-h" || $var == "-help" || $var == "-help" || $var == "--help" ]] ; then
help
return
fi
done
echo $PWD > $DIRLOADMOVE/.load_directory.$@
fi
}
function move {
for var in "$@" ; do
if [[ $var == "-h" || $var == "-help" || $var == "-help" || $var == "--help" ]] ; then
help
return
fi
done
if [ -e $DIRLOADMOVE/.load_directory.$@ ] ; then
cd `cat $DIRLOADMOVE/.load_directory.$@`
else
echo "No Loading file in $@"
fi
}
_moveload () # By convention, the function name
{
local cur
local cmd="${1##*/}"
_opts=$(list_all)
COMPREPLY=() # Array variable storing the possible completions.
cur=${COMP_WORDS[COMP_CWORD]}
prev="${COMP_WORDS[COMP_CWORD-1]}"
COMPREPLY=( $( compgen -W "${_opts}" -- ${cur} ))
return 0
}
complete -F _moveload move
complete -F _moveload load
# when session is closed, keep the `last` path
function finish {
# Your cleanup code here
if [ `pwd` != "$HOME" ] ; then
load last
fi
}
trap finish EXIT