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

dirs lists the contents of the directory stack (compare this with the $DIRSTACK variable). A<br />

successful pushd or popd will automatically invoke dirs.<br />

Scripts that require various changes to the current working directory without hard−coding the<br />

directory name changes can make good use of these commands. Note that the implicit $DIRSTACK<br />

array variable, accessible from within a script, holds the contents of the directory stack.<br />

Example 11−7. Changing the current working directory<br />

#!/bin/bash<br />

dir1=/usr/local<br />

dir2=/var/spool<br />

pushd $dir1<br />

# Will do an automatic 'dirs' (list directory stack to stdout).<br />

echo "Now in directory `pwd`." # Uses back−quoted 'pwd'.<br />

# Now, do some stuff in directory 'dir1'.<br />

pushd $dir2<br />

echo "Now in directory `pwd`."<br />

# Now, do some stuff in directory 'dir2'.<br />

echo "The top entry in the DIRSTACK array is $DIRSTACK."<br />

popd<br />

echo "Now back in directory `pwd`."<br />

# Now, do some more stuff in directory 'dir1'.<br />

popd<br />

echo "Now back in original working directory `pwd`."<br />

exit 0<br />

Variables<br />

let<br />

The let command carries out arithmetic operations on variables. In many cases, it functions as a less<br />

complex version of expr.<br />

Example 11−8. Letting let do some arithmetic.<br />

#!/bin/bash<br />

echo<br />

let a=11<br />

# Same as 'a=11'<br />

let a=a+5 # Equivalent to let "a = a + 5"<br />

# (double quotes and spaces make it more readable)<br />

echo "11 + 5 = $a"<br />

let "a

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

Saved successfully!

Ooh no, something went wrong!