#! /bin/bash

if [[ ! -b "$@" ]]; then
    echo "Usage: $0 device

Compares the start of each partition on device. Uses cfdisk, so works best with MBR, not say GPT." | fmt -w66
fi

function partition_starts() {
    cfdisk -Ps "$@" \
    | tail --lines=+6 \
    | sed 's|^[[:space:]]\+[[:digit:]]\+||g; s|[[:space:]]\+|,|g; s|\#||g' \
    | cut -d, -f3,5 \
    | sed 's|[^[:digit:]]\+|+|g' \
    | tr '\n' '; ' \
    | sed 's|\;$|\n|' \
    | bc
}

cmd=""
for n in $(partition_starts "$@"); do
    cmd="$cmd <(echo $n; dd if="$@" skip=$n count=1 2>/dev/null | xxd -c32 | awk '{print \$NF}'; echo)"
done

eval column $cmd
