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

declare −a IDX_DIG<br />

# BIG−DIR # DIR_ENT=( $(cat /tmpfs/junk2) )<br />

# BIG−DIR # DigestFile −if /tmpfs/junk2 IDX_DIG<br />

DigestFile DIR_ENT IDX_DIG<br />

# Small (should) be able to parallize IndexList & DigestFile<br />

# Large (should) be able to parallize IndexList & DigestFile & the assignment<br />

echo "The \"name\" (checksum) for the contents of ${PWD} is ${IDX_DIG[0]}"<br />

declare −a FILE_LOC<br />

LocateFile ${PWD} FILE_LOC<br />

ListArray FILE_LOC<br />

exit 0<br />

Stephane Chazelas demonstrates object−oriented programming in a Bash script.<br />

Example A−21. Object−oriented database<br />

#!/bin/bash<br />

# obj−oriented.sh: Object−oriented programming in a shell script.<br />

# Script by Stephane Chazelas.<br />

person.new() # Looks almost like a class declaration in C++.<br />

{<br />

local obj_name=$1 name=$2 firstname=$3 birthdate=$4<br />

}<br />

eval "$obj_name.set_name() {<br />

eval \"$obj_name.get_name() {<br />

echo \$1<br />

}\"<br />

}"<br />

eval "$obj_name.set_firstname() {<br />

eval \"$obj_name.get_firstname() {<br />

echo \$1<br />

}\"<br />

}"<br />

eval "$obj_name.set_birthdate() {<br />

eval \"$obj_name.get_birthdate() {<br />

echo \$1<br />

}\"<br />

eval \"$obj_name.show_birthdate() {<br />

echo \$(date −d \"1/1/1970 0:0:\$1 GMT\")<br />

}\"<br />

eval \"$obj_name.get_age() {<br />

echo \$(( (\$(date +%s) − \$1) / 3600 / 24 / 365 ))<br />

}\"<br />

}"<br />

$obj_name.set_name $name<br />

$obj_name.set_firstname $firstname<br />

$obj_name.set_birthdate $birthdate<br />

echo<br />

Appendix A. Contributed Scripts 405

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

Saved successfully!

Ooh no, something went wrong!