Posts Tagged ‘bash’

Bash script to fix indentation for C++ code

28 November 2012 Leave a comment

This small scripts allows me fix the indentation of C++ source files with very little effort.


  1. Drop one or more C++ source files in the “toindent” folder
  2. Run the script “./ toindent” (this calls the bcpp indenter to indent all files in the ‘toindent’ folder)
  3. Done

There are some options for step 2:

  • Select one file to indent: “ file.cpp”
  • Select all files in the ‘toindent’ folder: “./ toindent/”
  • Select only files with a certain extension in a folder (e.g. “.h”):  “./ toindent h”


Indentations options can be changed in the “bcpp_indenter.cfg” file.

Build script: course handouts from latex-beamer slides

4 December 2011 1 comment

As a lecturer, a part of my job consists of giving presentations using some kind of presentation software.

In the past, I uploaded a pdf file for each presentation to the  educational learing platform we use at our university. Typically 12+ files for one semester. Because some students prefer to print out the handouts, I provided a printer friendly version (8 slides per page), instantly doubling the number of files.

During the semester, I tend to adjust/tweak/update these files from time to time, making it dreadfull for me to update the files, and making it difficult for the student to ensure they have downloaded the last version of each file.

When I switched from to Latex beamer to prepare my presentations, I also decided to resolve this annoyance and add some extra features to my handouts.

This resulted in a build script.

What is it does:

  1. Build two pdf version for each lecture: presentation version and handout version (printer friendly colors, no slide transitions)
  2. Build a 8 slides per page (8pp) version starting from the handout version
  3. Create a nice handout document by joining all the 8pp files + include title page, introduction, table of contents
  4. Create a .zip archive file with all these documents

The end result is an updated .zip archive, with the last version of all the slides.  About once a week, I do a full build and upload this one file to our learning platform.

Sample commandline output:

$ ./build
Now processing: 0-hfdst0
Latex build succesfull
PDF 8pp build succesfull
Now processing: 7-hfdst7
Latex build succesfull
PDF 8pp build succesfull
Now processing: 8-hfdst8
Latex build succesfull
PDF 8pp build succesfull
Now joining all documents
Latex build succesfull
Build took 6 seconds.

Sample files:

(remove ‘.doc’ from the file extension, wordpress does not allow .zip/.sh)

Automated multiple choice exam grading

6 June 2011 4 comments

I wrote a little tool for automated multiple choice exam grading.

The idea:

  1. Students receive two sheets: one questions sheet and one answer sheet
  2. They indicate their correct answer by erasing the wrong answers (letters) on the answer sheet (see scan on the right)
  3. When all students have finished their exam, the answer sheets are batch-scanned and send to some email address.
  4. The server receives this email, starts processing the scans, calculates the grades and sends out a report to the lecturer

Read more…

Backup script: encrypted, offsite, incremental backup (rsync, rdiff-backup, encfs)

17 January 2011 2 comments

In this post I will explain a small (~100 lines) backup script I use to make offsite automated backups of several computers.

Overview of the scripts’ actions

  1. Mount a local folder using encfs (the backup files are written in this folder)
  2. Do an incremental backup with the local encfs folder from step one as the destination. Using rdiff-backup with a remote location as the target sometimes resulted in failed/broken backups on systems that frequently go offline. (laptops)
  3. Send the encrypted files to an off-site server (any server with ssh access will do, I use the quite cheap servers.
  4. Send all logged events and an overview of the incremental backups over email

Read more…

Arch Linux & Pacman

1 November 2009 Leave a comment

A week ago I switched from Debian 5.0 to Arch Linux. The initial learning curve was not that challenging as everything is perfectly documented in the ArchWiki.

The result is a snappy new system with very up to date packages, especially compared to Debian Stable.

One thing that kept bugging we was the syntax for the “Pacman” package manager. To resolve this small issue I added some aliases to $HOME/.bashrc

alias pacup="pacman -Syu" %(system update)
alias pacin="pacman -S" %(install packages)
alias pacinfo="pacman -Si" %(show package info)
alias pacrem="pacman -Rn" %(remove package)
alias pacmirror="reflector -l 8 -r -o /etc/pacman.d/mirrorlist" %(update repository list & rank fastest mirrors)
alias pacsearch="pacman -Sl | cut -d' ' -f2 | grep " % (search package database)

Backup using rdiff-backup: a bash select menu

23 February 2009 Leave a comment

This is a bash backup script, that uses rdiff-backup to perform “semi-automatic” backups. (I need to press two keys, to start a backup)

So, what does it do?

First there are some global options (mainly for debugging)
The exclude and include variables are links to the include/exclude files. (in this file you specify which folders to backup)
The “backup” variable: this is where rdiff-backup gets started.

Then a select menu is drawn (as described in a previous post.
The first option checks if my external HDD is mounted, if it is, it does the backup.
The second options checks if I am at home (based on IP), if I am it does the backup to a local file server.
The third option does the same, but on my work network. (it uses public IPs, so it is removed)
The fourth option (“l”), lists the different versions on my local backup.
The “p” option pulls some other backups that are automatically generated on a server to my laptop.

options="-v 5 --print-statistics "
exclude="--exclude-globbing-filelist $HOME/.scripts/tools/backup-lists/exclude"
include="--include-globbing-filelist $HOME/.scripts/tools/backup-lists/include"
backup="rdiff-backup $options $exclude/$target $include/$target --exclude '**' / $backuppath"
while :
echo "****************************************"
echo "* Backup using rdiff-backup *"
echo "****************************************"
echo "* [1] Backup to external hdd *"
echo "* [2] Backup to server @home *"
echo "* [3] Backup to work-server *"
echo "* [l] List increments ext hdd *"
echo "* [p] Pull backups work-server *"
echo "* [0] Exit/Stop *"
echo "****************************************"
echo -n "Enter your menu choice [1-5]: "
read yourch
case $yourch in
1) target="ext-hdd"
mount | grep /media/disk-1 > /dev/null
if [ $? -eq 0 ]
echo loop1
echo "Backup to external hdd finished, Press Enter to continue. . ."
echo "Error no external harddisk connected!"
echo "Press Enter to continue. . ."
exit 0;;
2) backuppath="server.local::/zfs-raidz/backup/autobackuplaptop/"
ifconfig | grep 192.168.123 > /dev/null
if [ $? -eq 0 ]
echo "Backup to ubuntuserver finished, Press Enter to continue. . ."
echo "You are not at home, ubuntuserver not reachable"
echo "Backup to ubuntuserver failed, Press Enter to continue. . ."
exit 0;;
3) backuppath="work...."
l) rdiff-backup -l --list-increment-sizes /media/disk-1/autobackuplaptop
echo "Press Enter to continue. . ."
exit 0;;
p) scp -rp -P 55555 me@ $HOME/data/backup
echo "Press Enter to continue. . ."
exit 0;;
0) exit 0;;
*) echo "Oopps!!! Please select 1,2,3,4, or 5";
echo "Press Enter to continue. . ." ; read ;;

Bash script: a simple select menu

17 February 2009 11 comments

As I started using Linux a couple years ago, I noticed I was moving to the command-line for many common tasks. When I noticed I was typing the same commands many times a day, I decided to to write some bash scripts to speed up some of these tasks.

The first part of these scripts is a simple select menu:

It shows several options, you select an option by typing a letter between brackets.
Some options start an other bash script, other options start python scripts.

More code will be released later.
– kdialog version, backup scripts, …

# Script to perform some common system operations
while :
echo "************************"
echo "* My tools *"
echo "************************"
echo "* [b] Backup *"
echo "* [a] Attach sshfs *"
echo "* [u] Unmount sshfs *"
echo "* [l] Server Login ssh *"
echo "* [t] Tunnels over ssh *"
echo "* [x] X-forwarding ssh *"
echo "* [y] YouTube download *"
echo "* [g] Portfolio grabber*"
echo "* [0] Exit/Stop *"
echo "************************"
echo -n "Enter your menu choice [a-0]: "
read yourch
case $yourch in
b) $HOME/.scripts/tools/backup ;;
a) $HOME/.scripts/tools/mount ;;
u) $HOME/.scripts/tools/umount ;;
l) $HOME/.scripts/tools/ssh ;;
t) $HOME/.scripts/tools/tunnel ;;
x) $HOME/.scripts/tools/xforward ;;
k) kate $HOME/.scripts/scripts ;;
g) $HOME/.scripts/tools/ ;;
y) python $HOME/.scripts/tools/ ;;
0) exit 0;;
*) echo "Oopps!!! Please select choice 1,2,3 or 4";
echo "Press Enter to continue. . ." ; read ;;