LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-03-2006, 01:13 PM   #1
human2.0
LQ Newbie
 
Registered: Aug 2005
Location: UK
Distribution: Mandriva 2005LE
Posts: 7

Rep: Reputation: 0
A shell script for optimising OpenDocument file sizes


Hi all,

Below is a shell script I have been writing on and off for a number of months. It's purpose is to reduce the space taken up by OpenDocument files, the format used by default by OOo and KOffice.

I'm not an expert programmer so any programming related suggestions would be welcome. I know some better error handling would help and some more loops rather than lists of commands.

Code:
#!/bin/bash
#######################################################################
# Author: Edward Holness                                              #
# Date: 2006-04-24                                                    #
# ScriptName: optimOD (Optimise OpenDocument)                         #
# Description:	A script that optimises OpenDocument (and *.sx* files,#
# 		with modification) losslessly.  It unzips the files,  #
#		optimises PNG and JPEG images, removes unneccessary   #
#		contents,then recompresses using higher compression   #
#		yet still being compatible with OpenDocument.         #
# Requirements:	7za, unzip, advpng, advzip, optipng, jpegoptim,       #
#		jpegtran and xmllint in your path.                    #
# Command line instructions: sh optimod <filename> or add to your path#
# Known bugs:                                                         #
#######################################################################

if test -f $1 		# test that file exists
	then echo $1 found		# confirmation of file existence
else
	echo no file to optimise, exiting nicely.	# warn that file doesn't exist and exit
	exit
fi

unzip -oq $1 -d optimod_$1	# Extract the file
echo $1 extracted to optimod_$1

#### Pictures
advpng -z4 optimod_$1/Pictures/*.png

echo running optipng...
for zw in 32k 16k 8k 4k 2k 1k 512
do
optipng -q -zw$zw -zc1-9 -zm1-9 -zs0-3 -f0-5 optimod_$1/Pictures/*.png
echo optipng $zw complete
done
echo PNG graphics optimised

jpegoptim --strip-all --quiet optimod_$1/Pictures/*.jpg	# Optimise JPEG's losslessly

## JPEG progressive
     for a in `ls optimod_$1/Pictures/*.jpg`
           do
jpegtran -progressive -outfile $a $a #.prog $a
done;
jpegoptim --strip-all --quiet optimod_$1/Pictures/*.jpg	# Optimise JPEG's losslessly
jpegoptim --strip-all -m95 --quiet optimod_$1/Pictures/*.jpg	# Cap JPEG's at m%

for a in `ls optimod_$1/Pictures/*.jpg`
           do
jpegtran -progressive -outfile $a $a #.prog $a
done;
jpegoptim --strip-all --quiet optimod_$1/Pictures/*.jpg	# Optimise JPEG's losslessly

echo JPEG graphics optimised

#### reduce contents, comment out for original content
rm optimod_$1/layout-cache
echo Removed layout-cache
rm optimod_$1/settings.xml
echo settings.xml removed
cd optimod_$1/
rm -rf Thumbnails/
echo Removed Thumbnails/
rm -rf Configurations2/
echo Removed Configurations2/
cd ../

#### content.xml
xmllint --nsclean --noblanks -o optimod_$1/content.xml.lint optimod_$1/content.xml
mv optimod_$1/content.xml.lint optimod_$1/content.xml
echo Compacted content.xml

#### styles.xml
xmllint --nsclean --noblanks -o optimod_$1/styles.xml.lint optimod_$1/styles.xml
mv optimod_$1/styles.xml.lint optimod_$1/styles.xml
echo Compacted styles.xml

#### meta.xml
xmllint --nsclean --noblanks -o optimod_$1/meta.xml.lint optimod_$1/meta.xml
mv optimod_$1/meta.xml.lint optimod_$1/meta.xml
echo Compacted meta.xml

#### manifest.xml
xmllint --nsclean --noblanks -o optimod_$1/META-INF/manifest.xml.lint optimod_$1/META-INF/manifest.xml
mv optimod_$1/META-INF/manifest.xml.lint optimod_$1/META-INF/manifest.xml
echo Compacted manifest.xml

#### Compress
cd optimod_$1/
zip -rq9DX ../$1.pass0 *
advzip ../$1.pass0 -z4
for ((  pass = 4 ;  pass <= 15;  pass++  ))
do

for ((  fb = 170 ;  fb <= 258;  fb++  ))
do
7za a -tzip -mfb$fb -mpass$pass ../$1.pass$pass.$fb * #-mx$x
#advzip ../$1.pass$pass.$fb -z4
done
done

for b in `ls -Sr ../$1.pass*`
do
mv $b ../$1
echo Compressed

#### Clear up the junk
rm -rf *
cd ../
rmdir optimod_$1/
rm $1.pass*
exit
done
 
Old 06-05-2006, 06:44 AM   #2
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
Code:
#### Compress
cd optimod_$1/

...

rm -rf *
I would defenitely put some error checking in there to make sure that cd has worked before doing rm -rf *
. That could be potentialy very bad
 
Old 06-05-2006, 06:47 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Just for readbility I'd indent the contents of 'if' blocks and 'for' loops.
 
Old 06-05-2006, 07:54 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
You could also condense a lot probably, take for instance "jpegoptim --strip-all --quiet" vs defining cmd="jpegoptim --strip-all --quiet" once and using $cmd, or the xmllint section. Only reason here BTW for function __optXml is not repetition but getting access to $FUNCNAME. Maybe add a generic returnvalue checker so you can break off and cleanup if something goes amiss and combine it with getopts for "-q" and "-v".

function __parseErr() { # retvalchk
case "$?" in 0) [ $verbose ] && echo "${t}: Optimised OK.";;
1) echo "Failed at $FUNCNAME" > /dev/stderr; cleanup; exit 1;;
*) echo "Got error value \"$?\". Buy a vowel NOW";; esac; }

function __optXml() { # Optimise XML files
for xmlfile in content.xml styles.xml meta.xml META-INF/manifest.xml; do
cmd="xmllint --nsclean --noblanks -o"; t="optimod_$1/$xmlfile"
$cmd "$t.lint" "$t" 2>&1>/dev/null; __parseErr && mv $t.lint $t; done; }
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
File Size Shell Script BlackLabel Programming 7 11-27-2007 07:48 PM
file handling shell script ankit_mcgill Programming 6 04-29-2006 12:32 AM
Linux file sizes (shell) Zeno McDohl Linux - General 8 06-03-2005 07:18 AM
Comparing file sizes using a bash script. IanChristie Programming 5 12-19-2003 10:14 PM
File Maintenance in a shell script? mdscudder Linux - General 1 02-20-2003 05:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration