forked from stm32duino/Arduino_Tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmassStorageCopy.sh
executable file
·93 lines (80 loc) · 2.28 KB
/
massStorageCopy.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
87
88
89
90
91
92
93
#!/bin/bash
set -o nounset # Treat unset variables as an error
# List
bin_filepath=
mountpoint_name=
mountpoint_path=
###############################################################################
## Help function
usage() {
echo "############################################################"
echo "##"
echo "## $(basename "$0") [-I <filepath>] [-O <mountpoint(s)> ]"
echo "##"
echo "## Options:"
echo "## -I: filepath binary to copy"
echo "## -O: mountpoint(s) destination name"
echo "## Could be a list (separated by','). Ex: \"NODE_1,NODE2,NODE_3\""
echo "## Note:"
echo "## -I and -O are optionals and kept for backward compatibility."
echo "############################################################"
exit "$1"
}
if [ $# -lt 2 ]; then
usage 1
fi
# Parsing options
if [ "$1" == "-I" ]; then
shift 1
fi
bin_filepath=$1
if [ "$2" == "-O" ]; then
shift 1
fi
# Strip first and last ""
mountpoint_name="${2%\"}"
mountpoint_name="${mountpoint_name#\"}"
if [ -z "$bin_filepath" ]; then
echo "No binary file path provided!"
exit 1
fi
if [ -z "$mountpoint_name" ]; then
echo "No mountpoint name provided!"
exit 1
fi
if [ ! -f "$bin_filepath" ]; then
echo "$bin_filepath not found!"
exit 2
fi
# Add short node name
IFS=' ,\t' read -ra mnt_list <<< "$mountpoint_name"
for mnt in "${mnt_list[@]}"; do
if [[ "$mnt" == "NODE_"* ]]; then
mountpoint_name="${mountpoint_name},${mnt//E_/_}"
fi
done
# Search the mountpoint
IFS=' ,\t' read -ra mnt_list <<< "$mountpoint_name"
for mnt in "${mnt_list[@]}"; do
mnt_path_list=($(df -H | grep -v "Mounted on" | rev | cut -d' ' -f1 | rev | sort -u | grep "$mnt"))
if [ ${#mnt_path_list[@]} -ne 0 ]; then
# Ensure to have exact match
for mnt_path in "${mnt_path_list[@]}"; do
mnt_name=$(echo "$mnt_path" | rev | cut -d'/' -f1 | rev)
if [ "$mnt_name" = "$mnt" ]; then
echo "Found '$mnt' at '$mnt_path'"
mountpoint_path=$mnt_path
break
fi
done
fi
done
if [ -z "$mountpoint_path" ] || [ ! -d "$mountpoint_path" ]; then
echo "$mountpoint_name not found."
echo "Please ensure the device is correctly connected and mounted."
exit 3
fi
# Copy the binary to the mountpoint
echo "Copying $bin_filepath to $mountpoint_path..."
cp "$bin_filepath" "$mountpoint_path"
exit $?