Bash Scripting Basics (Variable Assignment, If, Loop)


#!/bin/bash
##################################################
# Benchmark the processing time when wkhtmltopdf
# converts an html file to a pdf file
##################################################

if [[ $1 = "" || $2 = "" || $3 = "" ]]; then
echo "Usage: `basename $0` sourceFile.html destinationFile.pdf iteartionRunTime"
else
_min=9999
_max=0
_total=0
_iteration=$3
for (( _i=1; _i<=$_iteration; _i++ )) do _start_time=`date +%s` echo "running $_i" ~MYUSER/bin/benchmarkWkhtmltopdf.sh $1 $2 > /dev/null 2>&1
_end_time=`date +%s`
_processing_time=$((_end_time-_start_time))
if [ "$_processing_time" -lt "$_min" ]; then
_min=$_processing_time
fi
if [ "$_processing_time" -gt "$_max" ]; then
_max=$_processing_time
fi
_total=$(($_total+$_processing_time))
done
_processing_time_average=$(echo - | awk "{print $_total/$_iteration}")
echo "Processing time avg: $_processing_time_average min:$_min max:$_max total:$_total"
fi

Compress and Extract Files in Linux

Extract:

.tar.bz2
To extract a tar (Tape ARchiver) file, type the following in the shell prompt:

tar xvjf yourFileName.tar.bz2

Which will untar it to the current directory. Depending on the file structure that being compressed, it might create sub-directories.
Parameters:
x – extract
v – verbose output (lists all files as they are extracted)
j – deal with bzipped file
f – read from a file, rather than a tape device

.tar.gz or .tgz

tar xvzf file.tar.gz
tar xvzf file.tgz

Parameters:
-x, –extract, –get extract files from an archive
-v, –verbose verbosely list files processed
-z, –gzip, –gunzip, –ungzip filter the archive through gzip
-f, –file=ARCHIVE use archive file or device ARCHIVE

.bz2 file

tar jxf YOUR_FILE_NAME.bz2

.zip file

unzip YOUR_FILE_NAME.zip