Tuesday, June 21, 2016

[Ubuntu] Install missing LaTeX packages

Here is a script that installs missing LaTeX packages.


#!/bin/bash

EMPHASISCOLOR="\e[7m"
RESETEMPHASIS="\e[27m"
DEFAULTCOLOR="\e[39m"
GREENCOLOR="\e[32m"
LIGHGRAYCOLOR="\e[37m"
BLUECOLOR="\e[34m"
YELLOWCOLOR="\e[93m"

echo -e "************************************************************************"
echo -e "* LaTeX Package Installer"
echo -e "************************************************************************"
printf "* Checking dependencies....... "
# check if required programs are installed
hash apt-file 2>/dev/null || { echo >&2 "false\nI require apt-file but it's not installed.  Aborting."; exit 1; }
echo -e "${GREENCOLOR}ok${DEFAULTCOLOR}"


printf "* Packet name................. "
if [ -z "$1" ]; then 
echo -e "${YELLOWCOLOR}Please provide a packet name as argument$DEFAULTCOLOR"; 
exit 1
else 
PACKETNAME=$1; 
echo -e "${GREENCOLOR}$PACKETNAME$DEFAULTCOLOR"; 
fi

RESULT=`apt-file -x search "/${PACKETNAME}$"`

# if package is found, result is
# texlive-latex-extra: /usr/share/texlive/texmf-dist/tex/latex/preprint/balance.sty
# otherwise, result is empty
# exit code from apt-file is 0 in either case


IFS=':' read -a myarray <<< "$RESULT"

APTPACKAGE=${myarray[0]}

printf "* Result...................... "
if [[ $RESULT ]]; then
    echo -e "${GREENCOLOR}$APTPACKAGE$DEFAULTCOLOR"; 
else
    echo -e "${YELLOWCOLOR}no apt package found$DEFAULTCOLOR"; 
    exit 0
fi

echo "* Installing APT package...... "

sudo apt-get install $APTPACKAGE


echo -e "************************************************************************"
exit 0

Wednesday, June 15, 2016

Calculating statistics on the output EasyPMD's Copy/Paste Detector

Here is a shell script that computes the number of duplicate code segments and the total number of copied lines based on the output of EasyPMD's Copy/Paste Detector. This output is structured by segments starting with:

Found a x line (y tokens) duplication in the following files:

The script extracts these lines and sums up the occurrences and number of lines.



#!/bin/bash

if [[ $# -eq 0 ]] ; then
    echo 'Please specify a file name.'
    exit 0
fi

# Extract strings like:
# Found a X line (Y tokens) duplication in the following files: 
cat $1 | grep Found > tmp.txt    

# define counters
sum_lines=0
occurrences=0

# parse temp file and extract # of lines
while read line
do
    tmp=(`echo $line | tr ' ' ' '`)
    lines=${tmp[2]}
    sum_lines=`expr $sum_lines + $lines`
    occurrences=`expr $occurrences + 1`
done < tmp.txt

rm tmp.txt

echo "$occurrences code duplicates, $sum_lines lines in total."