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

}<br />

zz=`expr $zz + 1` # ==> Increment zz.<br />

done<br />

if [ −L "$dir" ] ; then # ==> If directory is a symbolic link...<br />

echo "+−−−$dir" `ls −l $dir | sed 's/^.*'$dir' //'`<br />

# ==> Display horiz. connector and list directory name, but...<br />

# ==> delete date/time part of long listing.<br />

else<br />

echo "+−−−$dir"<br />

# ==> Display horizontal connector symbol...<br />

# ==> and print directory name.<br />

if cd "$dir" ; then # ==> If can move to subdirectory...<br />

fi<br />

deep=`expr $deep + 1` # ==> Increment depth.<br />

search # with recursivity ;−)<br />

# ==> Function calls itself.<br />

numdirs=`expr $numdirs + 1` # ==> Increment directory count.<br />

fi<br />

fi<br />

done<br />

cd .. # ==> Up one directory level.<br />

if [ "$deep" ] ; then # ==> If depth = 0 (returns TRUE)...<br />

swfi=1<br />

# ==> set flag showing that search is done.<br />

fi<br />

deep=`expr $deep − 1` # ==> Decrement depth.<br />

# − Main −<br />

if [ $# = 0 ] ; then<br />

cd `pwd` # ==> No args to script, then use current working directory.<br />

else<br />

cd $1 # ==> Otherwise, move to indicated directory.<br />

fi<br />

echo "Initial directory = `pwd`"<br />

swfi=0 # ==> Search finished flag.<br />

deep=0 # ==> Depth of listing.<br />

numdirs=0<br />

zz=0<br />

while [ "$swfi" != 1 ] # While flag not set...<br />

do<br />

search # ==> Call function after initializing variables.<br />

done<br />

echo "Total directories = $numdirs"<br />

exit 0<br />

# ==> Challenge: try to figure out exactly how this script works.<br />

Noah Friedman gave permission to use his string function script, which essentially reproduces some of the<br />

C−library string manipulation functions.<br />

Example A−19. string functions: C−like string functions<br />

#!/bin/bash<br />

# string.bash −−− bash emulation of string(3) library routines<br />

# Author: Noah Friedman <br />

# ==> Used with his kind permission in this document.<br />

# Created: 1992−07−01<br />

# Last modified: 1993−09−29<br />

# Public domain<br />

Appendix A. Contributed Scripts 391

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

Saved successfully!

Ooh no, something went wrong!