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

DIRECTORY="/usr/X11R6/bin"<br />

# Try also "/bin", "/usr/bin", "/usr/local/bin", etc.<br />

for file in $DIRECTORY/*<br />

do<br />

whatis `basename $file`<br />

done<br />

# Echoes info about the binary.<br />

exit 0<br />

# You may wish to redirect output of this script, like so:<br />

# ./what.sh >>whatis.db<br />

# or view it a page at a time on stdout,<br />

# ./what.sh | less<br />

vdir<br />

See also Example 10−3.<br />

Show a detailed directory listing. The effect is similar to ls −l.<br />

This is one of the GNU fileutils.<br />

bash$ vdir<br />

total 10<br />

−rw−r−−r−− 1 bozo bozo 4034 Jul 18 22:04 data1.xrolo<br />

−rw−r−−r−− 1 bozo bozo 4602 May 25 13:58 data1.xrolo.bak<br />

−rw−r−−r−− 1 bozo bozo 877 Dec 17 2000 employment.xrolo<br />

bash ls −l<br />

total 10<br />

−rw−r−−r−− 1 bozo bozo 4034 Jul 18 22:04 data1.xrolo<br />

−rw−r−−r−− 1 bozo bozo 4602 May 25 13:58 data1.xrolo.bak<br />

−rw−r−−r−− 1 bozo bozo 877 Dec 17 2000 employment.xrolo<br />

locate, slocate<br />

The locate command searches for files using a database stored for just that purpose. The slocate<br />

command is the secure version of locate (which may be aliased to slocate).<br />

$bash locate hickson<br />

/usr/lib/xephem/catalogs/hickson.edb<br />

readlink<br />

Disclose the file that a symbolic link points to.<br />

strings<br />

bash$ readlink /usr/bin/awk<br />

../../bin/gawk<br />

Use the strings command to find printable strings in a binary or data file. It will list sequences of<br />

printable characters found in the target file. This might be handy for a quick 'n dirty examination of a<br />

core dump or for looking at an unknown graphic image file (strings image−file | more<br />

might show something like JFIF, which would identify the file as a jpeg graphic). In a script, you<br />

would probably parse the output of strings with grep or sed. See Example 10−7 and Example 10−9.<br />

Example 12−26. An "improved" strings command<br />

Chapter 12. External Filters, Programs and Commands 182

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

Saved successfully!

Ooh no, something went wrong!