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.

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

echo "Basename of /home/bozo/daily−journal.txt = `basename $a`"<br />

echo "Dirname of /home/bozo/daily−journal.txt = `dirname $a`"<br />

echo<br />

echo "My own home is `basename ~/`." # Also works with just ~.<br />

echo "The home of my home is `dirname ~/`." # Also works with just ~.<br />

exit 0<br />

split<br />

Utility for splitting a file into smaller chunks. Usually used for splitting up large files in order to back<br />

them up on floppies or preparatory to e−mailing or uploading them.<br />

sum, cksum, md5sum<br />

These are utilities for generating checksums. A checksum is a number mathematically calculated from<br />

the contents of a file, for the purpose of checking its integrity. A script might refer to a list of<br />

checksums for security purposes, such as ensuring that the contents of key system files have not been<br />

altered or corrupted. For security applications, use the 128−bit md5sum (message digest checksum)<br />

command.<br />

bash$ cksum /boot/vmlinuz<br />

1670054224 804083 /boot/vmlinuz<br />

bash$ md5sum /boot/vmlinuz<br />

0f43eccea8f09e0a0b2b5cf1dcf333ba /boot/vmlinuz<br />

Note that cksum also shows the size, in bytes, of the target file.<br />

Example 12−29. Checking file integrity<br />

#!/bin/bash<br />

# file−integrity.sh: Checking whether files in a given directory<br />

# have been tampered with.<br />

E_DIR_NOMATCH=70<br />

E_BAD_DBFILE=71<br />

dbfile=File_record.md5<br />

# Filename for storing records.<br />

set_up_database ()<br />

{<br />

echo ""$directory"" > "$dbfile"<br />

# Write directory name to first line of file.<br />

md5sum "$directory"/* >> "$dbfile"<br />

# Append md5 checksums and filenames.<br />

}<br />

check_database ()<br />

{<br />

local n=0<br />

local filename<br />

local checksum<br />

# −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− #<br />

# This file check should be unnecessary,<br />

Chapter 12. External Filters, Programs and Commands 187

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

Saved successfully!

Ooh no, something went wrong!