Skip to content

Instantly share code, notes, and snippets.

@0xJchen
Created September 17, 2023 05:15
Show Gist options
  • Save 0xJchen/c96d4c5f73672a7571a0867f35e09aba to your computer and use it in GitHub Desktop.
Save 0xJchen/c96d4c5f73672a7571a0867f35e09aba to your computer and use it in GitHub Desktop.
Auto Selection
#!/bin/bash
# Initialize an associative array to store png filenames and their corresponding grepped numbers
declare -A png_map
# Clear the loggers.txt file if it exists
> loggers.txt
# Iterate over each png file in ./selected directory
for png_file in ./selected/*.png; do
# Run pngtest on the current image file
./pngtest "$png_file"
# Run gcov and get the last line, then grep the percentage
grepped_number=$(gcov *.c 2>/dev/null | tail -n 1 | grep -o -E '[0-9]+\.[0-9]+')
# Get the base name of the png file (i.e., remove ./img/ prefix)
base_png_file=$(basename "$png_file")
# Write the png file name and the grepped number to loggers.txt
echo "$base_png_file $grepped_number" >> loggers.txt
# Store the grepped_number and corresponding png file in the associative array
png_map["$grepped_number"]="$base_png_file"
# Remove gcov data files and pngout.png to prepare for the next iteration
rm *.gcda pngout.png
done
# Sort the grepped numbers in descending order and store them in an array
sorted_keys=$(printf '%s\n' "${!png_map[@]}" | sort -rn)
# Initialize a rank counter
rank=1
# Copy the sorted png files to the newselected directory, rename them based on their rank, and print source and destination
for key in $sorted_keys; do
src="./selected/${png_map[$key]}"
dest="./newselected/rank${rank}.png"
# Print the source and destination before copying
echo "Copying $src to $dest"
cp "$src" "$dest"
rank=$((rank + 1))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment