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.

# cmp −s $1 $2 has same result ("−s" silent flag to "cmp")<br />

# Thank you Anders Gustavsson for pointing this out.<br />

#<br />

# Also works with 'diff', i.e., diff $1 $2 &> /dev/null<br />

if [ $? −eq 0 ]<br />

# Test exit status of "cmp" command.<br />

then<br />

echo "File \"$1\" is identical to file \"$2\"."<br />

else<br />

echo "File \"$1\" differs from file \"$2\"."<br />

fi<br />

exit 0<br />

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

comm<br />

Use zcmp on gzipped files.<br />

Versatile file comparison utility. The files must be sorted for this to be useful.<br />

Utilities<br />

comm −options first−file second−file<br />

comm file−1 file−2 outputs three columns:<br />

◊ column 1 = lines unique to file−1<br />

◊ column 2 = lines unique to file−2<br />

◊ column 3 = lines common to both.<br />

The options allow suppressing output of one or more columns.<br />

◊ −1 suppresses column 1<br />

◊ −2 suppresses column 2<br />

◊ −3 suppresses column 3<br />

◊ −12 suppresses both columns 1 and 2, etc.<br />

basename<br />

Strips the path information from a file name, printing only the file name. The construction<br />

basename $0 lets the script know its name, that is, the name it was invoked by. This can be used<br />

for "usage" messages if, for example a script is called with missing arguments:<br />

echo "Usage: `basename $0` arg1 arg2 ... argn"<br />

dirname<br />

Strips the basename from a filename, printing only the path information.<br />

basename and dirname can operate on any arbitrary string. The argument does not<br />

need to refer to an existing file, or even be a filename for that matter (see Example<br />

A−8).<br />

Example 12−28. basename and dirname<br />

#!/bin/bash<br />

a=/home/bozo/daily−journal.txt<br />

Chapter 12. External Filters, Programs and Commands 186

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

Saved successfully!

Ooh no, something went wrong!