#!/bin/bash # (c) Steve Parker, http://steve-parker.org/ # 19th March 2010 # Licensed under the GPL Version 2 # wrapper for dd to act like Solaris' mkfile utility. # mkfile adds "-n" (create empty file) and "-v" (verbose) which I can't # be bothered to implement. However, I have added "-b" to specify a blocksize, # and "-q" to be quiet function usage() { echo "Usage: mkfile [ -i infile ] [ -q ] [-b blocksize ] size[k|m|g] filename" echo "Blocksize is 512 bytes by default." exit 2 } function humanreadable () { multiplier=1 case $1 in *b) multiplier=1 ;; *k) multiplier=1024 ;; *m) multiplier=1048576 ;; *g) multiplier=1073741824 ;; esac numeric=`echo $1 | tr -d 'k'|tr -d 'm'|tr -d 'g'|tr -d 'b'` expr $numeric \* $multiplier } # mkfile uses 512 byte blocks by default - so shall we. bs=512 quiet=0 INFILE=/dev/zero while getopts 'i:b:q?' argv do case $argv in b) bs=$OPTARG ;; b) INFILE=$OPTARG ;; q) quiet=1 ;; \?) usage ;; *) usage ;; esac done for i in `seq 2 ${OPTIND}` do shift done if [ -z "$1" ]; then echo "ERROR: No size specificed" fi if [ -z "$2" ]; then echo "ERROR: No filename specificed" fi if [ "$#" -ne "2" ]; then usage fi SIZE=`humanreadable $1` FILENAME="$2" BS=`humanreadable $bs` COUNT=`expr $SIZE / $BS` CHECK=`expr $COUNT \* $BS` if [ "$CHECK" -ne "$SIZE" ]; then echo "Warning: Due to the blocksize requested, the file created will be `expr $COUNT \* $BS` bytes and not $SIZE bytes" fi echo -en "Creating $SIZE byte file $FILENAME...." dd if=$INFILE bs=$BS count=$COUNT of="$FILENAME" 2>/dev/null ddresult=$? if [ "$quiet" -ne "1" ]; then # We all know that you're goint to do this next - let's do it for you: if [ "$ddresult" -eq "0" ]; then echo "Finished:" else echo "An error occurred. dd returned code $ddresult." fi ls -l "$FILENAME" && ls -lh "$FILENAME" fi exit $ddresult