-
Notifications
You must be signed in to change notification settings - Fork 13
/
clip
executable file
·73 lines (63 loc) · 1.54 KB
/
clip
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
#!/bin/bash
# Copy symLink In Place
me=`basename $0`
usage () {
# Call as: usage [EXITCODE] [USAGE MESSAGE]
exit_code=1
if [[ "$1" == [0-9] ]]; then
exit_code="$1"
shift
fi
if [ -n "$1" ]; then
echo "$*" >&2
echo
fi
cat <<EOF >&2
Usage: $me [OPTIONS] SYMLINK [SYMLINK ...]
Options:
-v, --verbose Display successfully converted symlink(s)
-h, --help Show this help
EOF
exit "$exit_code"
}
if [ "$1" == '-h' ] || [ "$1" == '--help' ] || [ $# == 0 ]; then
usage 0
fi
verbose=
if [ "$1" == '-v' ] || [ "$1" == '--verbose' ]; then
verbose=y
shift
fi
main () {
for symlink in "$@"; do
if ! [ -e "$symlink" ]; then
echo "$symlink doesn't exist; skipping." >&2
continue
fi
if ! [ -L "$symlink" ]; then
echo "$symlink not a symlink; skipping." >&2
continue
fi
copy_symlink_in_place "$symlink"
done
}
copy_symlink_in_place () {
symlink="$1"
if ! temp=`mktemp "$symlink.$me.XXXXXXXXX"`; then
echo "ERROR: Failed to make temporary file; aborting." >&2
exit 1
fi
if ! cp -p "$symlink" "$temp"; then
echo "ERROR: Failed to copy $symlink to $temp; aborting." >&2
exit 1
fi
if mv "$temp" "$symlink"; then
if [ -n "$verbose" ]; then
echo "$symlink is now a copy of the original symlink target"
fi
else
echo "ERROR: Failed to rename $temp to $symlink; aborting." >&2
exit 1
fi
}
main "$@"