11.04.2013 Views

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

SHOW MORE
SHOW LESS

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

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

Appen<strong>di</strong>ce A. Script aggiuntivi<br />

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 ’-’ in<strong>di</strong>cating STDIN read.<br />

DigestFilesDoc<br />

DigestFile()<br />

{<br />

local if=0 # Default, variable name<br />

local -a T1 T2<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 />

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

650

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

Saved successfully!

Ooh no, something went wrong!