27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

# blotout.sh: Erase all traces of a file.<br />

# This script overwrites a target file alternately<br />

#+ with random bytes, then zeros before finally deleting it.<br />

# After that, even examining the raw disk sectors<br />

#+ will not reveal the original file data.<br />

PASSES=7<br />

BLOCKSIZE=1<br />

E_BADARGS=70<br />

E_NOT_FOUND=71<br />

E_CHANGED_MIND=72<br />

# Number of file−shredding passes.<br />

# I/O with /dev/urandom requires unit block size,<br />

#+ otherwise you get weird results.<br />

if [ −z "$1" ] # No filename specified.<br />

then<br />

echo "Usage: `basename $0` filename"<br />

exit $E_BADARGS<br />

fi<br />

file=$1<br />

if [ ! −e "$file" ]<br />

then<br />

echo "File \"$file\" not found."<br />

exit $E_NOT_FOUND<br />

fi<br />

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

echo; echo −n "Are you absolutely sure you want to blot out \"$file\" (y/n)? "<br />

read answer<br />

case "$answer" in<br />

[nN]) echo "Changed your mind, huh?"<br />

exit $E_CHANGED_MIND<br />

;;<br />

*) echo "Blotting out file \"$file\".";;<br />

esac<br />

flength=$(ls −l "$file" | awk '{print $5}') # Field 5 is file length.<br />

pass_count=1<br />

echo<br />

while [ "$pass_count" −le "$PASSES" ]<br />

do<br />

echo "Pass #$pass_count"<br />

sync<br />

# Flush buffers.<br />

dd if=/dev/urandom of=$file bs=$BLOCKSIZE count=$flength<br />

# Fill with random bytes.<br />

sync<br />

# Flush buffers again.<br />

dd if=/dev/zero of=$file bs=$BLOCKSIZE count=$flength<br />

# Fill with zeros.<br />

sync<br />

# Flush buffers yet again.<br />

let "pass_count += 1"<br />

echo<br />

done<br />

rm −f $file<br />

sync<br />

# Finally, delete scrambled and shredded file.<br />

# Flush buffers a final time.<br />

Chapter 12. External Filters, Programs and Commands 210

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!