-
Notifications
You must be signed in to change notification settings - Fork 6
/
uuidfstab.bash
executable file
·139 lines (112 loc) · 3.1 KB
/
uuidfstab.bash
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
# ----------------------------------------------------
# uuidfstab
#
# Converts device paths in a fstab file to UUID forms.
#
# Usage: uuidfstab[.bash] [--] fstab_file [output]
# uuidfstab[.bash] -h|--help|-V|--version
#
# Disclaimer: This tool comes with no warranty.
#
# Author: konsolebox
# Copyright Free / Public Domain
# May 5, 2022
# ----------------------------------------------------
[ -n "${BASH_VERSION}" ] || {
echo "Bash is needed to run this script." >&2
exit 1
}
set -f +o posix || exit 1
VERSION=2022.05.05
function log {
printf '%s\n' "$@" >&2
}
function show_usage_and_exit {
log "uuidfstab ${VERSION}
Converts device paths in a fstab file to UUID forms.
Usage: $0 [--] fstab_file [output_file]
$0 -h|--help|-V|--version
Notes:
- Results are saved back to the fstab_file if no output is specified.
- If '-' is specified as output, results are sent to stdout instead.
- Script messages are sent to stderr.
Disclaimer: This tool comes with no warranty."
exit 2
}
function fail {
log "$@"
exit 1
}
function main {
local non_opt_args
while [[ $# -gt 0 ]]; do
case $1 in
-V|--version)
log "${VERSION}"
return 2
;;
-h|--help)
show_usage_and_exit
;;
--)
non_opt_args+=("${@:2}")
break
;;
-[!-][!-]*)
set -- "${1:0:2}" "-${1:2}" "${@:2}"
continue
;;
-?*)
log "Invalid option: $1"
;;
*)
non_opt_args+=("$1")
;;
esac
shift
done
set -- "${non_opt_args[@]}"
[[ $# -eq 0 ]] && fail "No argument specified."
[[ $# -gt 2 ]] && fail "Too many arguments specified."
local fstab_file=$1 output_file=("${@:2}") actual_output_file=
[[ -e ${fstab_file} ]] || fail "File doesn't exist: ${fstab_file}"
[[ -f ${fstab_file} ]] || fail "Not a regular file: ${fstab_file}"
exec 3< "${fstab_file}" || fail "Failed to open '${fstab_file}' for reading."
if [[ -z ${output_file+.} ]]; then
log "Processing '${fstab_file}' and saving results to it."
elif [[ ${output_file} == - ]]; then
log "Processing '${fstab_file}' and writing results to stdout."
else
[[ -e ${output_file} && ! -f ${output_file} ]] && fail "Not a regular file: ${output_file}"
log "Processing '${fstab_file}' and writing results to '${output_file}'."
fi
exec 3< "${fstab_file}" || fail "Failed to open '${fstab_file}' for reading."
local line device output
while IFS= read -ru3 line; do
device=(${line})
if [[ $1 == /dev/* ]]; then
id=$(blkid "${device}" -s UUID -o value) || \
fail "Error occurred while trying to get UUID of '${device}'."
if [[ -n ${id} ]]; then
output[${#output[@]}]="# ${device} = ${id}"
output[${#output[@]}]="${line/${device}/UUID=${id}}"
continue
fi
fi
output[${#output[@]}]=${line}
done
exec 3<&- || fail "Failed to close '${fstab_file}'."
if [[ -z ${output_file+.} ]]; then
printf '%s\n' "${output[@]}" >"${fstab_file}" || \
fail "Failed to write data to '${fstab_file}'."
elif [[ ${output_file} == - ]]; then
printf '%s\n' "${output[@]}"
else
printf '%s\n' "${output[@]}" >"${output_file}" || \
fail "Failed to write data to '${output_file}'."
fi
log "Done."
return 0
}
main "$@"