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

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

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

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

msg=filename<br />

else<br />

msg=$@<br />

fi<br />

}<br />

echo "Usage: `basename $0` "$msg""<br />

Check_if_root () # Check if root running script.<br />

{ # From "ex39.sh" example.<br />

if [ "$UID" −ne "$ROOT_UID" ]<br />

then<br />

echo "Must be root to run this script."<br />

exit $E_NOTROOT<br />

fi<br />

}<br />

CreateTempfileName () # Creates a "unique" temp filename.<br />

{ # From "ex51.sh" example.<br />

prefix=temp<br />

suffix=`eval date +%s`<br />

Tempfilename=$prefix.$suffix<br />

}<br />

isalpha2 ()<br />

# Tests whether *entire string* is alphabetic.<br />

{ # From "isalpha.sh" example.<br />

[ $# −eq 1 ] || return $FAILURE<br />

}<br />

case $1 in<br />

*[!a−zA−Z]*|"") return $FAILURE;;<br />

*) return $SUCCESS;;<br />

esac<br />

# Thanks, S.C.<br />

abs ()<br />

# Absolute value.<br />

{ # Caution: Max return value = 256.<br />

E_ARGERR=−999999<br />

if [ −z "$1" ]<br />

then<br />

return $E_ARGERR<br />

fi<br />

# Need arg passed.<br />

# Obvious error value returned.<br />

if [ "$1" −ge 0 ]<br />

# If non−negative,<br />

then #<br />

absval=$1<br />

# stays as−is.<br />

else<br />

# Otherwise,<br />

let "absval = (( 0 − $1 ))" # change sign.<br />

fi<br />

}<br />

return $absval<br />

tolower ()<br />

# Converts string(s) passed as argument(s)<br />

{ #+ to lowercase.<br />

if [ −z "$1" ]<br />

# If no argument(s) passed,<br />

Chapter 34. Miscellany 348

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

Saved successfully!

Ooh no, something went wrong!