Skip to content

Instantly share code, notes, and snippets.

@gamma
Created March 4, 2019 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gamma/725ec468cf45c3ff73edf39ee5a84807 to your computer and use it in GitHub Desktop.
Save gamma/725ec468cf45c3ff73edf39ee5a84807 to your computer and use it in GitHub Desktop.
MacOS bash dd command with progress
function isodump {
FILE=$1
DISK=$2
if [ -z "$FILE" ] || [ -z "$DISK" ] ; then
echo "Usage: isodump <ISO File> <DEVICE>"
return 128
fi
if [ ! -f "$FILE" ] ; then
echo "File: '$FILE' not found!"
return 1
fi
if [ ! -e "/dev/r$DISK" ] ; then
echo "Device '$DISK' not found!"
return 2
fi
echo "You have to authenticate as root to proceed"
sudo echo "Thx."
FILSIZE=`stat -f "%z" "$FILE"`
FILSIZE=$(( FILSIZE/1000/1000 ))
echo "Transfering: ${FILSIZE}MB ..."
LOG=`mktemp ${TMPDIR:-/tmp/}isodump.XXXXXX`
# Create Subshell to suppress output of background job, also fetch job id in new pipe
(sudo dd if="$FILE" of=/dev/r$DISK bs=1m 2> "$LOG" & echo $! >3 )
bgid=`cat <3`
while ps -p $bgid > /dev/null ; do
sleep 1
sudo kill -INFO $bgid 2>&1 >/dev/null || break
sleep 1
LOGLINE=`tail -n 1 "$LOG"`
if [ ! -z "$LOGLINE" ] ; then
BYTES=`echo $LOGLINE | awk '{print $2}'`
if [ "bytes" == "$BYTES" ] ; then
WRITTEN=`echo $LOGLINE | awk '{print $1}'`
WRITTEN=$(( WRITTEN/1000/1000 ))
SPEED=`echo $LOGLINE | awk '{print $7}'`
SPEED=${SPEED:1}
SPEED=$(( SPEED/1000/1000 ))
TIME=`echo $LOGLINE | awk '{print $5}'`
echo "${WRITTEN}MB / ${FILSIZE}MB (${SPEED} MB/s - running for ${TIME%.*}s)"
fi
fi
sleep 3
done
rm "$LOG"
}
export -f isodump
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment