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.

The md5sum program is documented as outputting three fields (and it<br />

does), but when read it appears as two fields (array elements). This<br />

is caused by the lack of whitespace between the second and third field.<br />

So this function gropes the md5sum output and returns:<br />

[0] 32 character checksum in hexidecimal (UCFS filename)<br />

[1] Single character: ' ' text file, '*' binary file<br />

[2] Filesystem (20th Century Style) name<br />

Note: That name may be the character '−' indicating STDIN read.<br />

DigestFilesDoc<br />

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

DigestFile()<br />

{<br />

local if=0<br />

local −a T1 T2<br />

# Default, variable name<br />

case "$#" in<br />

3) case "$1" in<br />

−if) if=1 ; shift ;;<br />

* ) return 1 ;;<br />

esac ;;<br />

2) : ;; # Poor man's "continue"<br />

*) return 1 ;;<br />

esac<br />

case $if in<br />

0) eval T1=\( \"\$\{$1\[@\]\}\" \)<br />

T2=( $(echo ${T1[@]} | md5sum −) )<br />

;;<br />

1) T2=( $(md5sum $1) )<br />

;;<br />

esac<br />

case ${#T2[@]} in<br />

0) return 1 ;;<br />

1) return 1 ;;<br />

2) case ${T2[1]:0:1} in # SanScrit−2.0.5<br />

\*) T2[${#T2[@]}]=${T2[1]:1}<br />

T2[1]=\*<br />

;;<br />

*) T2[${#T2[@]}]=${T2[1]}<br />

T2[1]=" "<br />

;;<br />

esac<br />

;;<br />

3) : ;; # Assume it worked<br />

*) return 1 ;;<br />

esac<br />

}<br />

local −i len=${#T2[0]}<br />

if [ $len −ne 32 ] ; then return 1 ; fi<br />

eval $2=\( \"\$\{T2\[@\]\}\" \)<br />

# # # # # Locate File # # # # #<br />

#<br />

# LocateFile [−l] FileName Location−Array−Name<br />

# or<br />

# LocateFile [−l] −of FileName Location−Array−FileName<br />

# # # # #<br />

Appendix A. Contributed Scripts 401

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

Saved successfully!

Ooh no, something went wrong!