-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.sh
More file actions
38 lines (32 loc) · 899 Bytes
/
utils.sh
File metadata and controls
38 lines (32 loc) · 899 Bytes
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
#!/usr/bin/env bash
# Utilities for other scripts
set -euo pipefail
# Usage:
# link_same TARGET LINK_NAME
#
# Create a link to TARGET only if LINK_NAME does not exist
# if LINK_NAME exists and links to TARGET, then do nothing
# otherwise, fail
link_same() {
mkdir -p "$(dirname "$2")"
(ln -sT "$1" "$2" &> /dev/null && echo "Linked $2 -> $1") \
|| ([ "$(readlink "$2")" = "$1" ] && echo "Already linked $2") \
|| (echo -e "\033[0;31mUnable to link $2 -> $1\033[0m" && false)
}
# Usage:
# link_same_files SOURCE_DIR TARGET_DIR
#
# Similar to link_same, but links each file inside of TARGET
link_same_files() {
for f in "$1"/*; do
link_same "$f" "$2/$(basename "$f")"
done
}
# Usage
# link_same_single SOURCE_DIR FILENAME TARGET_DIR
#
# Links the file with the semantics of link_same and creates
# the directory if not exists
link_same_single() {
link_same "$1/$2" "$3/$2"
}