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

exit 0<br />

Piping output to a read, using echo to set variables will fail.<br />

However, piping the output of cat does seem to work.<br />

cat file1 file2 |<br />

while read line<br />

do<br />

echo $line<br />

done<br />

Filesystem<br />

cd<br />

The familiar cd change directory command finds use in scripts where execution of a command<br />

requires being in a specified directory.<br />

(cd /source/directory && tar cf − . ) | (cd /dest/directory && tar xpvf −)<br />

[from the previously cited example by Alan Cox]<br />

The −P (physical) option to cd causes it to ignore symbolic links.<br />

cd − changes to $OLDPWD, the previous working directory.<br />

The cd command does not function as expected when presented with two forward<br />

slashes.<br />

bash$ cd //<br />

bash$ pwd<br />

//<br />

The output should, of course, be /. This is a problem both from the command line and<br />

in a script.<br />

pwd<br />

Print Working Directory. This gives the user's (or script's) current directory (see Example 11−7). The<br />

effect is identical to reading the value of the builtin variable $PWD.<br />

pushd, popd, dirs<br />

This command set is a mechanism for bookmarking working directories, a means of moving back and<br />

forth through directories in an orderly manner. A pushdown stack is used to keep track of directory<br />

names. Options allow various manipulations of the directory stack.<br />

pushd dir−name pushes the path dir−name onto the directory stack and simultaneously<br />

changes the current working directory to dir−name<br />

popd removes (pops) the top directory path name off the directory stack and simultaneously changes<br />

the current working directory to that directory popped from the stack.<br />

Chapter 11. Internal Commands and Builtins 131

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

Saved successfully!

Ooh no, something went wrong!