-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymlinks
executable file
·83 lines (73 loc) · 1.4 KB
/
symlinks
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
BASENAME=$(which basename)
ECHO=/bin/echo
FIND=$(which find)
LS=/bin/ls
SED=$(which sed)
progname=$(${BASENAME} $0)
function printusage() {
${ECHO} "usage: ${progname} { -a | [-bds] } <directory> [ <directory> ... ]" >&2
exit 1
}
absolute=""
all=""
dangling=""
while getopts abds flag; do
case $flag in
s)
absolute="yes"
;;
b|d)
dangling="yes"
;;
a)
all="yes"
;;
?)
printusage
;;
esac
done
shift $(( OPTIND - 1 ))
if [ -z "${all}" -a -z "${absolute}" -a -z "${dangling}" ]
then
all="yes"
elif [ -n "${all}" ] && [ -n "${absolute}" -o -n "${dangling}" ]
then
${ECHO} "error: can't use -a with -b, -d or -s" >&2
printusage
fi
if [ $# -eq 0 ]
then
${ECHO} "error: must supply at least one directory as an argument" >&2
printusage
fi
for arg in "$@"
do
${FIND} ${arg} -type l -print | \
while read line
do
string=$(${LS} -l "${line}")
destination=$(${ECHO} ${string} | ${SED} -e 's/^.* -> //')
if [ ! -e "${line}" ]
then
if [ -n "${all}" -o -n "${dangling}" ]
then
${ECHO} "[31mdangling[0m: ${line} -> ${destination}"
fi
# Don't put the glob in quotes or it will be treated
# as a literal string.
elif [[ "${destination}" == /* ]]
then
if [ -n "${all}" -o -n "${absolute}" ]
then
${ECHO} "[33mabsolute[0m: ${line} -> ${destination}"
fi
else
if [ -n "${all}" ]
then
${ECHO} "${line} -> ${destination}"
fi
fi
done
done