Skip to content

Commit d04462d

Browse files
author
Alex J Lennon
committed
fix: Address shellcheck warnings and improve code quality
- Add -r flag to all read commands to prevent backslash mangling - Quote glob expansions in for loops to prevent word splitting - Separate variable declarations from assignments to avoid masking return values - Fix parameter expansion quoting for proper string manipulation - Resolve subshell variable modification issue with temporary file approach - Improve overall shell script robustness and POSIX compliance All shellcheck warnings and errors now resolved.
1 parent 42c609b commit d04462d

1 file changed

Lines changed: 32 additions & 15 deletions

File tree

wic-editor.sh

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ ask_confirmation() {
144144
fi
145145

146146
if [ "$default" = "y" ]; then
147-
read -p "$message [Y/n]: " response
147+
read -r -p "$message [Y/n]: " response
148148
case "$response" in
149149
[nN]|[nN][oO]) return 1 ;;
150150
*) return 0 ;;
151151
esac
152152
else
153-
read -p "$message [y/N]: " response
153+
read -r -p "$message [y/N]: " response
154154
case "$response" in
155155
[yY]|[yY][eE][sS]) return 0 ;;
156156
*) return 1 ;;
@@ -165,7 +165,7 @@ select_partition_interactive() {
165165
list_partitions "$loop_device"
166166

167167
echo "Which partition would you like to modify?"
168-
read -p "Enter partition number: " partition_choice
168+
read -r -p "Enter partition number: " partition_choice
169169

170170
# Validate partition choice
171171
partition_device="${loop_device}p${partition_choice}"
@@ -192,7 +192,7 @@ find_partition_by_label() {
192192
local target_label="$2"
193193

194194
# Check each partition for matching label
195-
for part in ${loop_device}p*; do
195+
for part in "${loop_device}p"*; do
196196
if [ -e "$part" ]; then
197197
label=$(sudo blkid -o value -s LABEL "$part" 2>/dev/null || echo "")
198198
if [ "$label" = "$target_label" ]; then
@@ -211,7 +211,7 @@ find_partition_by_filesystem() {
211211
local target_fs="$2"
212212

213213
# Check each partition for matching filesystem
214-
for part in ${loop_device}p*; do
214+
for part in "${loop_device}p"*; do
215215
if [ -e "$part" ]; then
216216
fs_type=$(sudo blkid -o value -s TYPE "$part" 2>/dev/null || echo "")
217217
if [ "$fs_type" = "$target_fs" ]; then
@@ -231,7 +231,7 @@ find_largest_partition() {
231231
local largest_size=0
232232

233233
# Check each partition
234-
for part in ${loop_device}p*; do
234+
for part in "${loop_device}p"*; do
235235
if [ -e "$part" ]; then
236236
# Get partition size in bytes
237237
size=$(sudo blockdev --getsize64 "$part" 2>/dev/null || echo "0")
@@ -256,13 +256,15 @@ select_target_partition() {
256256
local ROOT_PARTITION=""
257257
local LARGEST_SIZE=0
258258

259-
for part in ${loop_device}p*; do
259+
for part in "${loop_device}p"*; do
260260
if [ -e "$part" ]; then
261261
# Get filesystem type
262-
local FS_TYPE=$(sudo blkid -o value -s TYPE "$part" 2>/dev/null || echo "unknown")
262+
local FS_TYPE
263+
FS_TYPE=$(sudo blkid -o value -s TYPE "$part" 2>/dev/null || echo "unknown")
263264

264265
# Get partition size
265-
local SIZE=$(sudo blockdev --getsize64 "$part" 2>/dev/null || echo "0")
266+
local SIZE
267+
SIZE=$(sudo blockdev --getsize64 "$part" 2>/dev/null || echo "0")
266268

267269
# Look for ext4 filesystem (common for root partition)
268270
if [ "$FS_TYPE" = "ext4" ] && [ "$SIZE" -gt "$LARGEST_SIZE" ]; then
@@ -295,7 +297,8 @@ select_target_partition() {
295297
return 1
296298
fi
297299

298-
local partition=$(find_partition_by_label "$loop_device" "$TARGET_PARTITION")
300+
local partition
301+
partition=$(find_partition_by_label "$loop_device" "$TARGET_PARTITION")
299302
if [ -z "$partition" ]; then
300303
echo "Error: Partition with label '$TARGET_PARTITION' not found"
301304
return 1
@@ -310,7 +313,8 @@ select_target_partition() {
310313
return 1
311314
fi
312315

313-
local partition=$(find_partition_by_filesystem "$loop_device" "$TARGET_PARTITION")
316+
local partition
317+
partition=$(find_partition_by_filesystem "$loop_device" "$TARGET_PARTITION")
314318
if [ -z "$partition" ]; then
315319
echo "Error: Partition with filesystem '$TARGET_PARTITION' not found"
316320
return 1
@@ -364,17 +368,30 @@ delete_files_from_partition() {
364368
pattern=$(basename "$file_pattern")
365369

366370
if [ -d "$base_dir" ]; then
371+
# Use a temporary file to count deletions since we're in a subshell
372+
local temp_count_file="/tmp/wic_editor_count_$"
373+
echo "$files_deleted" > "$temp_count_file"
374+
367375
find "$base_dir" -name "$pattern" -type f -print0 | while IFS= read -r -d '' found_file; do
368-
relative_file=${found_file#$mount_dir}
376+
relative_file=${found_file#"$mount_dir"}
369377

370378
if [ "$FORCE_OVERWRITE" = true ] || ask_confirmation "Delete file: $relative_file?" "n"; then
371379
sudo rm -f "$found_file"
372380
echo " Deleted: $relative_file"
373-
((files_deleted++))
381+
# Update count in temp file
382+
local current_count
383+
current_count=$(cat "$temp_count_file")
384+
echo $((current_count + 1)) > "$temp_count_file"
374385
else
375386
echo " Skipped: $relative_file"
376387
fi
377388
done
389+
390+
# Read back the count
391+
if [ -f "$temp_count_file" ]; then
392+
files_deleted=$(cat "$temp_count_file")
393+
rm -f "$temp_count_file"
394+
fi
378395
else
379396
echo " Directory not found: $(dirname "$file_pattern")"
380397
((files_not_found++))
@@ -443,7 +460,7 @@ handle_file_conflict() {
443460
return 0
444461
fi
445462

446-
read -p " Choose option [1-4]: " choice
463+
read -r -p " Choose option [1-4]: " choice
447464
case "$choice" in
448465
1)
449466
echo " Action: Overwriting existing file"
@@ -486,7 +503,7 @@ copy_files_with_conflict_handling() {
486503
# Find all files in source directory
487504
while IFS= read -r -d '' src_file; do
488505
# Get relative path from source directory
489-
relative_path="${src_file#$src_dir/}"
506+
relative_path="${src_file#"$src_dir"/}"
490507
dst_file="$dst_dir/$relative_path"
491508

492509
# Check if destination file exists

0 commit comments

Comments
 (0)