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.

docbook−dtd41−sgml−1.0−10<br />

docbook−utils−0.6.9−2<br />

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

cpio<br />

This specialized archiving copy command (copy input and output) is rarely seen any more, having<br />

been supplanted by tar/gzip. It still has its uses, such as moving a directory tree.<br />

Example 12−22. Using cpio to move a directory tree<br />

#!/bin/bash<br />

# Copying a directory tree using cpio.<br />

ARGS=2<br />

E_BADARGS=65<br />

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

then<br />

echo "Usage: `basename $0` source destination"<br />

exit $E_BADARGS<br />

fi<br />

source=$1<br />

destination=$2<br />

find "$source" −depth | cpio −admvp "$destination"<br />

# Read the man page to decipher these cpio options.<br />

exit 0<br />

rpm2cpio<br />

This command extracts a cpio archive from an rpm one.<br />

Example 12−23. Unpacking an rpm archive<br />

#!/bin/bash<br />

# de−rpm.sh: Unpack an 'rpm' archive<br />

: ${1?"Usage: `basename $0` target−file"}<br />

# Must specify 'rpm' archive name as an argument.<br />

TEMPFILE=$$.cpio<br />

# Tempfile with "unique" name.<br />

# $$ is process ID of script.<br />

rpm2cpio < $1 > $TEMPFILE<br />

# Converts rpm archive into cpio archive.<br />

cpio −−make−directories −F $TEMPFILE −i # Unpacks cpio archive.<br />

rm −f $TEMPFILE<br />

# Deletes cpio archive.<br />

exit 0<br />

# Exercise:<br />

# Add check for whether 1) "target−file" exists and<br />

#+ 2) it is really an rpm archive.<br />

# Hint: parse output of 'file' command.<br />

Compression<br />

Chapter 12. External Filters, Programs and Commands 178

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

Saved successfully!

Ooh no, something went wrong!