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...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

check_database<br />

# Do the actual work.<br />

echo<br />

# You may wish to redirect the stdout of this script to a file,<br />

#+ especially if the directory checked has many files in it.<br />

# For a much more thorough file integrity check,<br />

#+ consider the "Tripwire" package,<br />

#+ http://sourceforge.net/projects/tripwire/.<br />

exit 0<br />

shred<br />

See also Example A−20 for a creative use of the md5sum command.<br />

Securely erase a file by overwriting it multiple times with random bit patterns before deleting it. This<br />

command has the same effect as Example 12−42, but does it in a more thorough and elegant manner.<br />

This is one of the GNU fileutils.<br />

<strong>Advanced</strong> forensic technology may still be able to recover the contents of a file, even<br />

after application of shred.<br />

Encoding and Encryption<br />

uuencode<br />

This utility encodes binary files into ASCII characters, making them suitable for transmission in the<br />

body of an e−mail message or in a newsgroup posting.<br />

uudecode<br />

This reverses the encoding, decoding uuencoded files back into the original binaries.<br />

Example 12−30. uudecoding encoded files<br />

#!/bin/bash<br />

lines=35<br />

# Allow 35 lines for the header (very generous).<br />

for File in * # Test all the files in the current working directory...<br />

do<br />

search1=`head −$lines $File | grep begin | wc −w`<br />

search2=`tail −$lines $File | grep end | wc −w`<br />

# Uuencoded files have a "begin" near the beginning,<br />

#+ and an "end" near the end.<br />

if [ "$search1" −gt 0 ]<br />

then<br />

if [ "$search2" −gt 0 ]<br />

then<br />

echo "uudecoding − $File −"<br />

uudecode $File<br />

fi<br />

fi<br />

done<br />

# Note that running this script upon itself fools it<br />

#+ into thinking it is a uuencoded file,<br />

Chapter 12. External Filters, Programs and Commands 189

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

Saved successfully!

Ooh no, something went wrong!