Letterhead/Adressen/address.sh

111 lines
2.2 KiB
Bash
Executable File

#! /bin/bash
# address takes two arguments: a flag what to do as $1 and a handle to handle as $2
H=$2
F=$1
O=false
LOC=""
COL=""
if [ $F = "-l" ] # -l möchte kein weiteres Argument, im Gegensatz zu allen anderen Optionen.
then
echo "handle: Name"
while IFS="," read -r COL1 COL2 COLX
do
echo "$COL1: $COL2"
done < <(tail -n +2 Adressen.csv)
exit 0
fi
if [ $# -lt 2 ]
then
echo "Zu wenig Argumente"
exit 1
fi
if [ $# -gt 2 ]
then
echo "Ignoriere überschüssige Argumente"
fi
if [ $F = "-n" ]
then
COL=Name
O=true
fi
if [ $F = "-a" ]
then
COL=Anrede
O=true
fi
if [ $F = "-d" ]
then
COL=Adresse
O=true
fi
if [ $F = "-f" ]
then
# return fax number to be used by sipgate-cli
COL=FaxNr
O=true
fi
if [ $F = "-t" ]
then
COL=TelNr
O=true
fi
if [ $F = "-c" ]
then
COL=Closing
O=true
fi
if [ $F = "-p" ]
then
COL=parent
O=true
fi
if [ $O = true ]
then
LOC=$(head -1 Adressen.csv | tr ',' '\n' | nl |grep -w "$COL" | tr -d " " | awk -F " " '{print $1}') #calculating position of requested Col
grep "$H" Adressen.csv | head -n 1 | cut -d "," -f$LOC | sed 's/"//g'
#Returning requested column, making sure to return only one result (even though redundant results shouldn't be possible)
exit 0
fi
if [ $F = "-g" ]
then
NULL=$(grep "^$H" Adressen.csv)
ISH=$? #exit code. Der if-Block wird ausgeführt, wenn etwas gefunden wurde. (ISH steht für is handle)
if [ $ISH -ne 1 ]
then
echo "Handle schon vergeben"
exit 1
fi
echo "==========Generate Address for handle $H=================="
read -p 'Name: ' NAME
read -p 'Anrede (default: "Damen und Herren"): ' ANREDE
read -p 'Straße und Hausnummer: ' STREET
read -p 'PLZ und Ort: ' CITY
read -p 'Faxnummer: ' FAX
read -p 'Telefonnummer: ' TEL
read -p 'Schlussformel (default: "Mit der Ihnen gebührenden Hochachtung"):' CLOSING
read -p 'Nächsthöhere Hierarchieebene (handle): ' PARENT
ANREDE=${ANREDE:-"Damen und Herren"}
CLOSING=${CLOSING:-"Mit der Ihnen gebührenden Hochachtung"}
echo "$H,$NAME,$ANREDE,$STREET\\\\$CITY,$FAX,$TEL,$CLOSING,$PARENT" >> Adressen.csv
NULL=$(grep "^$PARENT" Adressen.csv) # findet alles, wenn $PARENT leer ist.
ISP=$? #exit code. Der if-Block wird ausgeführt, wenn nichts gefunden wurde.
if [ $ISP -eq 1 ]
then
eval $0 -g $PARENT
fi
fi