12

image Wrapping gzip in a Shell Script

Here is a sample script that uses the gzip command-line tool to compress a file. Because it is wrapped in a script, we can add a check that the file was compressed properly by uncompressing it again and comparing the result with the original. If the compression was successful, the original file is removed. Otherwise, the abortive compressed file is discarded.

SOURCE_FILE=$1 gzip -9c ${SOURCE_FILE} > ${SOURCE_FILE}.gz LINE_COUNT=‘cat ${SOURCE_FILE} | wc -l’ ZIPP_COUNT=‘gzip -dc ${SOURCE_FILE}.gz | wc –l’ if [ ${LINE_COUNT} -eq ${ZIPP_COUNT} ] then rm ${SOURCE_FILE} else rm ${SOURCE_FILE}.gz fi

Although this ...

Get Developing Quality Metadata now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.