-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·83 lines (73 loc) · 1.93 KB
/
install.sh
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
#!/bin/bash
script_cdirs="cdirs.sh"
# get_absolute_path <path>
get_absolute_path() {
echo $(echo "$(cd "$1" &>/dev/null && pwd)" | sed 's/\(.*\)\/$/\1/g')
}
check_cdirs() {
src="`pwd`/${script_cdirs}"
#check existed
[ ! -f ${src} ] && {
src="$(get_absolute_path $(dirname $0))/${script_cdirs}"
[ ! -f ${src} ] && {
return 1
}
}
chmod u+x ${src}
return 0
}
print_help() {
echo "Usage: ./install.sh [-h|--help] [--uninstall] [--unalias-cd]"
}
uninstall() {
[ -f ~/.bashrc ] && sed -i '/set for cdirs/,/end for cdirs/d' ~/.bashrc
[ -f ~/.bash_logout ] && sed -i '/set for cdirs/,/end for cdirs/d' ~/.bash_logout
}
if [ "$#" -gt 1 ]; then
print_help
exit 1
elif [ "$#" -eq 1 ]; then
case "$1" in
--uninstall)
uninstall
echo -e "\033[31mcdirs has unistalled, have a fun day\033[0m"
exit 0
;;
--unalias-cd)
unalias_cd=1
[ -f ~/.bashrc ] && sed -i '/set for cdir/,/end for cdir/d' ~/.bashrc
shift
;;
--help|-h)
print_help
exit 0
;;
*)
print_help
exit 1
;;
esac
fi
echo -n "finding cdirs.sh ... "
check_cdirs && echo YES || {
echo NO
echo -e "\033[31mcan not find cdirs.sh\033[0m"
exit 1
}
uninstall
echo -n "setting cdirs to ~/.bashrc ... "
cat >> ~/.bashrc <<EOF
# == set for cdirs ==
source ${src}$([ "${unalias_cd}" = "1" ] && echo " --unalias-cd")
# == end for cdirs ==
EOF
echo "YES"
echo -n "setting cdirs to ~/.bash_logout ... "
cat >> ~/.bash_logout <<EOF
# == set for cdirs ==
[ -n "\${gmpy_cdirs_env}" ] && rm \${gmpy_cdirs_env}
# == end for cdirs ==
EOF
echo "YES"
echo -e "\033[31mcdirs has installed, please reload ~/.bashrc <source ~/.bashrc>\033[0m"
echo -e "\033[32msee more $([ -z "${not_replace_cd}" ] && echo "cd|")cdir|setdir|lsdir|cldir --help\033[0m"