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

watch<br />

bash$ ldd /bin/ls<br />

libc.so.6 => /lib/libc.so.6 (0x4000c000)<br />

/lib/ld−linux.so.2 => /lib/ld−linux.so.2 (0x80000000)<br />

Run a command repeatedly, at specified time intervals.<br />

The default is two−second intervals, but this may be changed with the −n option.<br />

strip<br />

nm<br />

rdist<br />

watch −n 5 tail /var/log/messages<br />

# Shows tail end of system log, /var/log/messages, every five seconds.<br />

Remove the debugging symbolic references from an executable binary. This decreases its size, but<br />

makes debugging it impossible.<br />

This command often occurs in a Makefile, but rarely in a shell script.<br />

List symbols in an unstripped compiled binary.<br />

Remote distribution client: synchronizes, clones, or backs up a file system on a remote server.<br />

Using our knowledge of administrative commands, let us examine a system script. One of the shortest and<br />

simplest to understand scripts is killall, used to suspend running processes at system shutdown.<br />

Example 13−8. killall, from /etc/rc.d/init.d<br />

#!/bin/sh<br />

# −−> Comments added by the author of this document marked by "# −−>".<br />

# −−> This is part of the 'rc' script package<br />

# −−> by Miquel van Smoorenburg, <br />

# −−> This particular script seems to be Red Hat specific<br />

# −−> (may not be present in other distributions).<br />

# Bring down all unneeded services that are still running (there shouldn't<br />

# be any, so this is just a sanity check)<br />

for i in /var/lock/subsys/*; do<br />

# −−> Standard for/in loop, but since "do" is on same line,<br />

# −−> it is necessary to add ";".<br />

# Check if the script is there.<br />

[ ! −f $i ] && continue<br />

# −−> This is a clever use of an "and list", equivalent to:<br />

# −−> if [ ! −f "$i" ]; then continue<br />

# Get the subsystem name.<br />

subsys=${i#/var/lock/subsys/}<br />

# −−> Match variable name, which, in this case, is the file name.<br />

# −−> This is the exact equivalent of subsys=`basename $i`.<br />

# −−> It gets it from the lock file name (if there is a lock file,<br />

# −−>+ that's proof the process has been running).<br />

# −−> See the "lockfile" entry, above.<br />

Chapter 13. System and Administrative Commands 234

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

Saved successfully!

Ooh no, something went wrong!