-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink.sh
executable file
·61 lines (54 loc) · 1.34 KB
/
link.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
#!/bin/bash
BACKUP="$HOME/backup"
YELLOW="\E[1;33m"
GREEN="\E[1;32m"
RED="\E[1;31m"
RESET="\E[m"
function run_with_status() {
"$@" &> /dev/null
local status=$?
if [ $status -ne 0 ]; then
echo -e "${RED}Error with $@${RESET}" >&2
else
echo -e "${GREEN}Successfully ran $@${RESET}"
fi
}
function link_file() {
if [[ -f "$1" || -d "$1" ]]; then
if [[ -d "$HOME/$1" ]]; then
rm -r "$HOME/$1"
fi
if [[ -e "$HOME/$1" ]]; then
echo -e "${YELLOW}Backing up $HOME/$1...${RESET}"
run_with_status mv "$HOME/$1" "$BACKUP/"
fi
base=$(dirname "$HOME/$1")
if [[ ! -d $base ]]; then
mkdir -p "$base"
fi
echo -e "${YELLOW}Creating symlink: $PWD/$1 -> $HOME/$1"
run_with_status ln -s "$PWD/$1" "$HOME/$1"
else
echo -e "${RED}Not a valid file!${RESET}"
fi
}
if [[ ! "$PWD" =~ .*dotfiles ]]; then
echo -e "${RED}Please run this script from the root dotfiles directory"
exit 1
fi
if [[ ! -d "$BACKUP" ]]; then
mkdir -p "$BACKUP"
fi
if [[ $# != 0 ]]; then
# Link files listed as args
for file in "$@"; do
link_file "$file"
done
else
while :; do
ls -a
echo -n "What file/folder would you like to link? >> "
read file
link_file "$file"
done
fi