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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

present).<br />

bash$ diff −r ~/notes1 ~/notes2<br />

Only in /home/bozo/notes1: file02<br />

Only in /home/bozo/notes1: file03<br />

Only in /home/bozo/notes2: file04<br />

diff3<br />

Use zdiff to compare gzipped files.<br />

An extended version of diff that compares three files at a time. This command returns an exit value of<br />

0 upon successful execution, but unfortunately this gives no information about the results of the<br />

comparison.<br />

bash$ diff3 file−1 file−2 file−3<br />

====<br />

1:1c<br />

This is line 1 of "file−1".<br />

2:1c<br />

This is line 1 of "file−2".<br />

3:1c<br />

This is line 1 of "file−3"<br />

sdiff<br />

cmp<br />

Compare and/or edit two files in order to merge them into an output file. Because of its interactive<br />

nature, this command would find little use in a script.<br />

The cmp command is a simpler version of diff, above. Whereas diff reports the differences between<br />

two files, cmp merely shows at what point they differ.<br />

Like diff, cmp returns an exit status of 0 if the compared files are identical, and 1 if<br />

they differ. This permits use in a test construct within a shell script.<br />

Example 12−27. Using cmp to compare two files within a script.<br />

#!/bin/bash<br />

ARGS=2 # Two args to script expected.<br />

E_BADARGS=65<br />

E_UNREADABLE=66<br />

if [ $# −ne "$ARGS" ]<br />

then<br />

echo "Usage: `basename $0` file1 file2"<br />

exit $E_BADARGS<br />

fi<br />

if [[ ! −r "$1" || ! −r "$2" ]]<br />

then<br />

echo "Both files to be compared must exist and be readable."<br />

exit $E_UNREADABLE<br />

fi<br />

cmp $1 $2 &> /dev/null # /dev/null buries the output of the "cmp" command.<br />

Chapter 12. External Filters, Programs and Commands 185

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

Saved successfully!

Ooh no, something went wrong!