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

# This simple−minded cipher can be broken by an average 12−year old<br />

#+ using only pencil and paper.<br />

exit 0<br />

tr variants<br />

fold<br />

fmt<br />

The tr utility has two historic variants. The BSD version does not use brackets (tr a−z A−Z), but<br />

the SysV one does (tr '[a−z]' '[A−Z]'). The GNU version of tr resembles the BSD one, so<br />

quoting letter ranges within brackets is mandatory.<br />

A filter that wraps lines of input to a specified width. This is especially useful with the −s option,<br />

which breaks lines at word spaces (see Example 12−19 and Example A−2).<br />

Simple−minded file formatter, used as a filter in a pipe to "wrap" long lines of text output.<br />

Example 12−19. Formatted file listing.<br />

#!/bin/bash<br />

WIDTH=40<br />

b=`ls /usr/local/bin`<br />

# 40 columns wide.<br />

# Get a file listing...<br />

echo $b | fmt −w $WIDTH<br />

# Could also have been done by<br />

# echo $b | fold − −s −w $WIDTH<br />

exit 0<br />

See also Example 12−4.<br />

A powerful alternative to fmt is Kamil Toman's par utility, available from<br />

http://www.cs.berkeley.edu/~amc/Par/.<br />

col<br />

This deceptively named filter removes reverse line feeds from an input stream. It also attempts to<br />

replace whitespace with equivalent tabs. The chief use of col is in filtering the output from certain text<br />

processing utilities, such as groff and tbl.<br />

column<br />

Column formatter. This filter transforms list−type text output into a "pretty−printed" table by inserting<br />

tabs at appropriate places.<br />

Example 12−20. Using column to format a directory listing<br />

#!/bin/bash<br />

# This is a slight modification of the example file in the "column" man page.<br />

Chapter 12. External Filters, Programs and Commands 174

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

Saved successfully!

Ooh no, something went wrong!