-
Notifications
You must be signed in to change notification settings - Fork 4
/
ch.sh
87 lines (71 loc) · 1.31 KB
/
ch.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
84
85
86
#!/bin/bash
# Author: xnum
# Email: xnumtw@gmail.com
# Date: 2017/5/4
REAL_HOME=$(cat /etc/passwd | grep $USER | awk -F ':' '{ print $6 }')
WORK_DIR="$REAL_HOME/.xnws"
load_ws() {
if [ ! -d "$WORK_DIR/$1" ] ; then
echo "Error: workspace [$1] is not exists"
return
fi
export ORIG_PS1="$PS1"
export PS1="[$1] $PS1"
export HOME="$WORK_DIR/$1"
rsync -auq --exclude ".xnws" $REAL_HOME/.* $HOME
cd ~
}
exit_ws() {
export PS1=$ORIG_PS1
export HOME=$REAL_HOME
cd ~
}
create_ws() {
if [ ! -d $WORK_DIR ] ; then
mkdir $WORK_DIR
fi
if [ ! -d "$WORK_DIR/$1" ] ; then
mkdir $WORK_DIR/$1
fi
load_ws $1
}
usage() {
echo "usage: ch (new|load|exit|ls) [name]"
echo "\tnew [name]"
echo "\tload [name]"
echo "\tls"
echo "\texit"
}
case "$1" in
new)
if [ $# -ne 2 ]; then
usage
else
create_ws $2
fi
;;
load)
if [ "$REAL_HOME" != "$HOME" ] ; then
echo "Error: You are at virtual home"
else
if [ $# -ne 2 ]; then
usage
else
load_ws $2
fi
fi
;;
exit)
if [ "$REAL_HOME" = "$HOME" ] ; then
echo "Error: You are at real home"
else
exit_ws
fi
;;
ls)
ls -1 $WORK_DIR
;;
*)
usage
;;
esac