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

day=$?<br />

strip_leading_zero $month<br />

month=$?<br />

day_index $day $month $year<br />

date2=$?<br />

abs $date2<br />

date2=$value<br />

# Make sure it's positive.<br />

calculate_difference $date1 $date2<br />

abs $diff<br />

diff=$value<br />

# Make sure it's positive.<br />

echo $diff<br />

exit 0<br />

# Compare this script with the implementation of Gauss' Formula in C at<br />

# http://buschencrew.hypermart.net/software/datedif<br />

Example A−9. Make a "dictionary"<br />

#!/bin/bash<br />

# makedict.sh [make dictionary]<br />

# Modification of /usr/sbin/mkdict script.<br />

# Original script copyright 1993, by Alec Muffett.<br />

#<br />

# This modified script included in this document in a manner<br />

#+ consistent with the "LICENSE" document of the "Crack" package<br />

#+ that the original script is a part of.<br />

# This script processes text files to produce a sorted list<br />

#+ of words found in the files.<br />

# This may be useful for compiling dictionaries<br />

#+ and for lexicographic research.<br />

E_BADARGS=65<br />

if [ ! −r "$1" ]<br />

then<br />

echo "Usage: $0 files−to−process"<br />

exit $E_BADARGS<br />

fi<br />

# Need at least one<br />

#+ valid file argument.<br />

# SORT="sort" # No longer necessary to define options<br />

#+ to sort. Changed from original script.<br />

cat $* |<br />

# Contents of specified files to stdout.<br />

tr A−Z a−z |<br />

# Convert to uppercase.<br />

tr ' ' '\012' |<br />

# New: change spaces to newlines.<br />

# tr −cd '\012[a−z][0−9]' | # Get rid of everything non−alphanumeric<br />

#+ (original script).<br />

tr −c '\012a−z' '\012' | # Rather than deleting<br />

#+ now change non−alpha to newlines.<br />

sort |<br />

# $SORT options unnecessary now.<br />

uniq |<br />

# Remove duplicates.<br />

Appendix A. Contributed Scripts 376

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

Saved successfully!

Ooh no, something went wrong!