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

sleep 3 h<br />

# Pauses 3 hours!<br />

usleep<br />

The watch command may be a better choice than sleep for running commands at<br />

timed intervals.<br />

Microsleep (the "u" may be read as the Greek "mu", or micro− prefix). This is the same as sleep,<br />

above, but "sleeps" in microsecond intervals. It can be used for fine−grain timing, or for polling an<br />

ongoing process at very frequent intervals.<br />

usleep 30<br />

# Pauses 30 microseconds.<br />

This command is part of the Red Hat initscripts / rc−scripts package.<br />

The usleep command does not provide particularly accurate timing, and is therefore<br />

unsuitable for critical timing loops.<br />

hwclock, clock<br />

The hwclock command accesses or adjusts the machine's hardware clock. Some options require root<br />

privileges. The /etc/rc.d/rc.sysinit startup file uses hwclock to set the system time from<br />

the hardware clock at bootup.<br />

The clock command is a synonym for hwclock.<br />

12.4. Text Processing Commands<br />

Commands affecting text and text files<br />

sort<br />

tsort<br />

uniq<br />

File sorter, often used as a filter in a pipe. This command sorts a text stream or file forwards or<br />

backwards, or according to various keys or character positions. Using the −m option, it merges<br />

presorted input files. The info page lists its many capabilities and options. See Example 10−9,<br />

Example 10−10, and Example A−9.<br />

Topological sort, reading in pairs of whitespace−separated strings and sorting according to input<br />

patterns.<br />

This filter removes duplicate lines from a sorted file. It is often seen in a pipe coupled with sort.<br />

cat list−1 list−2 list−3 | sort | uniq > final.list<br />

# Concatenates the list files,<br />

# sorts them,<br />

# removes duplicate lines,<br />

# and finally writes the result to an output file.<br />

The useful −c option prefixes each line of the input file with its number of occurrences.<br />

bash$ cat testfile<br />

This line occurs only once.<br />

This line occurs twice.<br />

This line occurs twice.<br />

Chapter 12. External Filters, Programs and Commands 160

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

Saved successfully!

Ooh no, something went wrong!