Automate boring stuff with bash
...Synology, aruba VPS, cron, WebDav, and some nerdiness!

Architettura

Synology

  • A casa mia (on premise)
  • Backup su amazon glacier
  • Accessibile dall'esterno solo sulle porte dei servizi attivi
  • Synology DiskStation (Dropbox/OwnCloud clone)

Aruba VPS

  • Non indispensabile
  • Sempre attivo
  • Può erogare anche altri servizi utili (es: git web hooks..)
  • Fa girare gli script necessari in cron o syscontrol

Mobile / PC / Mac

  • Un qualsiasi dispositivo che abbia un client DiskStation
  • o WebDav
  • o qualsiasi altra cosa per cui ci sia un server installabile su VPS

Come funziona

Demo time

💣

Runner

#!/bin/bash

for i in *; do
  echo -ne "Testing $i:"
  for s in $HOME/.scripts/automation.d/*.sh; do
      echo -ne "$(basename $s) ("
    $s "$i"
    echo -ne "), "
  done
  echo -ne "\n"
done

Actions - spese

#!/bin/bash

DEST="$HOME/SynologyDrive/Documents/Documenti Personali/Unico"

function rename_and_move () {
  YEAR=`echo "$1" | cut -c 1-4`
  [[ ! -f "$DEST/$YEAR" ]] && mkdir -p "$DEST/$YEAR"
  mv "$1" "$DEST/$YEAR/$(basename $1)-testcamp.pdf"
}

FILE=$1

if [[ ! -f $FILE ]] || [[ $FILE != *"spese mediche"* ]]; then echo -ne "N"; exit 0;fi

rename_and_move "$FILE"
echo -ne "Y"
YEAR=`echo "$1" | cut -c 1-4`

mv "$1" "$DEST/$YEAR/$(basename $1)-testcamp.pdf"
  • cut
  • `
  • $(...)
if [[ ! -f $FILE ]] || [[ $FILE != *"spese mediche"* ]]; then echo -ne "N"; exit 0;fi
  • if
  • *"..."*
  • echo -ne

Actions - telepass

#!/bin/bash

DEST="$HOME/SynologyDrive/Documents/Documenti Lavoro"

function rename_and_move () {
  DATA=`pdftotext "$1" - | awk '/DATA/ { getline; print $3 }'`
  YEAR=`echo $DATA | cut -d'-' -f 3`
  MONTH=`echo $DATA | cut -d'-' -f 2`
  DAY=`echo $DATA | cut -d'-' -f 1`
  [[ ! -f "$DEST/$YEAR/Spese/$MONTH" ]] && mkdir -p "$DEST/$YEAR/Spese/$MONTH"
  mv $1 "$DEST/$YEAR/Spese/$MONTH/$YEAR-$MONTH-$DAY-telepass-testcamp.pdf"
}

FILE=$1


if [[ ! -f $FILE ]] || [[ ! ${FILE: -4} == ".pdf" ]]; then echo -ne "N"; exit 0;fi

MATCH=$(pdftotext "$FILE" - | grep TELEPASS)
if [ ! -z "$MATCH" ]; then
  rename_and_move "$FILE"
  echo -ne "Y"
else
    echo -ne "N"
fi
DATA=`pdftotext "$1" - | awk '/DATA/ { getline; print $3 }'`
  • awk
  • pdftotext
  • "..."
YEAR=`echo $DATA | cut -d'-' -f 3`
  • cut -d
if [[ ! -f $FILE ]] || [[ ! ${FILE: -4} == ".pdf" ]]; then echo -ne "N"; exit 0;fi
  • ${FILE: -4}

Conclusioni

  • Con bash potete fare di tutto
  • Il limite è solo la vostra fantasia
  • Con bash potete spaccare tutto
  • Usate sudo con parsimonia
  • RTFM (man-page)
  • e ricordate...