-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.sh
More file actions
57 lines (54 loc) · 1.45 KB
/
util.sh
File metadata and controls
57 lines (54 loc) · 1.45 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
#!/bin/bash
info() {
printf "\033[34m$1\\033[39m$2"
}
warn() {
printf "\033[33m$1\\033[39m$2"
}
# Links file ./$1/$2 to ~/$2
symlink_home () {
if [ ! -e ~/$2 ]; then
dir=`pwd`/
cd ~
info "Creating symbolic link for ~/$2\n"
# echo ln -s $dir$1$2 $2
ln -s $dir$1$2 $2
cd $dir
fi
}
symlink() {
# If the file exists and it's not a link, ask if we should remove it
if [[ -e "$3/$4" && ! -L "$3/$4" ]]; then # If it's not a symlink
read -p "File $3/$4 exists. Merge? [y/N]" -r
# echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
if [[ -d "$3/$4" ]]; then
# If we could remove it, it was empty
if ! rmdir "$3/$4" ; then
# If it's a directory, move all the content and rmdir
warn "Moving $3/$4/* to $1/$2/\n"
mv "$3/$4/"* "$3/$4/".* "$1/$2/"
rmdir "$3/$4"
fi
else
# if it's just a file, then just move it
warn "Moving $3/$4 to $1/$2\n"
mv "$3/$4" "$1/$2"
fi
fi
fi
# If the file does not exist, link it
if [ ! -e "$3/$4" ]; then
dir=`pwd`/
# If the directory to link to does not exist, create it
if [ ! -e $3 ]; then
mkdir -p $3
fi
cd $3
info "Creating symbolic link for $3/$4\n"
# echo ln -s $dir$1$2 $4
ln -s $dir$1$2 $4
cd $dir
fi
}