-
Notifications
You must be signed in to change notification settings - Fork 1
/
dotlink.sh
executable file
·87 lines (73 loc) · 1.71 KB
/
dotlink.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/sh
usage()
{
cat >&2 <<EOF
usage: $(basename $0) [-h] [-b FILE] </path/from/> </path/to/>
EOF
exit 1
}
help_()
{
cat >&2 <<EOF
USAGE
$(basename $0) [OPTIONS]... </path/from> </path/to>
DESCRIPTION
$(basename $0) links files from one directory
to another.
EXAMPLE
Link all files from current directory to home,
ignoring files listed in .gitignore.
$(basename $0) -b .gitignore . \$HOME
OPTIONS
-b, --black-list=FILE
Ignore files/directories matching the ones
listed in FILE.
-h, --help
Show this help.
EOF
exit 0
}
wrong()
{
printf '%s\n' "$(basename $0): $1"
usage
}
while test $# -gt 0
do
case "$1" in
-b|--black-list)
BLACK_LIST="$2"
shift 2
;;
-h|--help)
help_
;;
*)
[ -d "$1" ] || \
wrong "file $1 doesn't exist, or isn't a directory."
[ -z "$FILE_FROM" ] && {
FILE_FROM="$1"
} || {
[ -z "$FILE_TO" ] && {
FILE_TO="$1"
} || {
usage
}
}
shift 1
;;
esac
done
test -n "$FILE_FROM" || wrong "missing </path/from> argument."
test -n "$FILE_TO" || wrong "missing </path/to> argument."
ls -A "$FILE_FROM" | while read FILE
do
# check whether file is contained in black-list.
# if so, skip it.
[ -f "$BLACK_LIST" ] && grep -q "$FILE" "$BLACK_LIST" && {
printf '%s\n' "skipping $FILE_FROM/$FILE"
continue
}
printf '%s\n' "linking $FILE_FROM/$FILE -> $FILE_TO/.$FILE"
ln -f -s "$(realpath "$FILE_FROM/$FILE")" "$FILE_TO/.$FILE"
done