#!/bin/sh if test "$#" -ne 2; then echo 1>&2 "Usage: $0 infile outdevice" exit 1 fi if ! test -e $1; then echo 1>&2 "$0: input file $1 is not exists" exit 1 fi if ! test -f $1; then echo 1>&2 "$0: input file $1 is not regular file" exit 1 fi if ! test -e $2; then echo 1>&2 "$0: output device file $2 is not exists" exit 1 fi if ! test -b $2; then echo 1>&2 "$0: output device file $2 is not block special file" exit 1 fi size=`stat --format=%s $1` msize=$(( $size / 1024 / 1024 )) tmpfile=tmp.bin logfile=ddv.log trap "rm -f $tmpfile" EXIT res() { if test $? -eq 0; then echo "OK" else echo "failed" exit 1 fi } dd_progress= dd_ver=`dd --version | fgrep 'dd (coreutils)' | cut '-d ' -f3` || true #echo "dd_ver: $dd_ver" dd_ver_major=`echo $dd_ver | cut -d. -f2` #echo "dd_ver_major: $dd_ver_major" if test "$dd_ver_major" -ge 8; then dd_ver_minor=`echo $dd_ver | cut -d. -f2` #echo "minor: $dd_ver_minor" if test "$dd_ver_minor" -ge 26; then dd_progress="status=progress" fi fi #echo "dd_progress: $dd_progress" if ! test -w $2; then echo 1>&2 "$0: output device file $2 is not writable, Use sudo." exit 1 fi echo -n "Writing ... " rm -f $logfile dd if=$1 of=$2 bs=1M 2>>$logfile res echo -n "Unplug card reader ... " while test -b $2; do sleep 0.1; done echo "unplugged" echo -n "Plug card reader in again ... " while ! test -b $2 -a -r $2; do sleep 0.1; done echo "plugged" echo -n "Reading ... " rm -f $tmpfile dd if=$2 of=$tmpfile bs=1M count=$msize 2>>$logfile res echo -n "Comparing ... " cmp --quiet $1 $tmpfile res echo "All OK"