2021-03-13 16:44:33 +00:00
|
|
|
#! /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=""
|
2021-03-14 12:58:09 +00:00
|
|
|
BASEPATH=$(dirname $(realpath $0))
|
2021-03-13 16:44:33 +00:00
|
|
|
|
2021-04-07 17:33:29 +00:00
|
|
|
function usage {
|
|
|
|
echo "Usage:
|
|
|
|
./address.sh <option> <handle>
|
|
|
|
Option is exactly one of the following:
|
|
|
|
-h Print this message und exit
|
|
|
|
-l List all available handles and names.
|
|
|
|
With those two options, no <handle> ist needed.
|
|
|
|
-g Generate new entry
|
|
|
|
-n Print name
|
|
|
|
-o Print preferred Opening
|
|
|
|
-c Print preferred Closing
|
|
|
|
-a Print address
|
|
|
|
-f Print fax number
|
|
|
|
-t Print telephone number
|
2021-04-07 19:18:26 +00:00
|
|
|
-p Print parent handle
|
|
|
|
-e Edit entry"
|
2021-04-07 17:33:29 +00:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2021-04-07 19:18:26 +00:00
|
|
|
if [ "$F" = "-l" ] # -l nimmt kein weiteres Argument, im Gegensatz zu allen anderen Optionen.
|
2021-03-13 18:51:38 +00:00
|
|
|
then
|
|
|
|
echo "handle: Name"
|
|
|
|
while IFS="," read -r COL1 COL2 COLX
|
|
|
|
do
|
|
|
|
echo "$COL1: $COL2"
|
2021-03-14 12:58:09 +00:00
|
|
|
done < <(tail -n +2 $BASEPATH/Adressen.csv)
|
2021-03-13 18:51:38 +00:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2021-04-07 17:33:29 +00:00
|
|
|
if [ "$F" = "-h" ] # -h auch nicht, um genau zu sein.
|
|
|
|
then
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
2021-03-13 16:44:33 +00:00
|
|
|
if [ $# -lt 2 ]
|
|
|
|
then
|
2021-04-07 17:33:29 +00:00
|
|
|
echo "Zu wenig Argumente
|
|
|
|
"
|
|
|
|
usage
|
2021-03-13 16:44:33 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ $# -gt 2 ]
|
|
|
|
then
|
2021-04-07 19:18:26 +00:00
|
|
|
echo "Ignoriere überschüssige Argumente
|
|
|
|
"
|
2021-04-07 17:33:29 +00:00
|
|
|
usage
|
2021-03-13 16:44:33 +00:00
|
|
|
fi
|
|
|
|
|
2021-03-14 12:58:09 +00:00
|
|
|
case $F in
|
|
|
|
"-n")
|
2021-03-13 16:44:33 +00:00
|
|
|
COL=Name
|
|
|
|
O=true
|
2021-03-14 12:58:09 +00:00
|
|
|
;;
|
2021-03-13 16:44:33 +00:00
|
|
|
|
2021-04-07 17:33:29 +00:00
|
|
|
"-o")
|
2021-03-13 16:44:33 +00:00
|
|
|
COL=Anrede
|
|
|
|
O=true
|
2021-03-14 12:58:09 +00:00
|
|
|
;;
|
2021-03-13 16:44:33 +00:00
|
|
|
|
2021-04-07 17:33:29 +00:00
|
|
|
"-a")
|
2021-03-13 16:44:33 +00:00
|
|
|
COL=Adresse
|
|
|
|
O=true
|
2021-03-14 12:58:09 +00:00
|
|
|
;;
|
2021-03-13 16:44:33 +00:00
|
|
|
|
2021-03-14 12:58:09 +00:00
|
|
|
"-f")
|
2021-03-13 16:44:33 +00:00
|
|
|
# return fax number to be used by sipgate-cli
|
|
|
|
COL=FaxNr
|
|
|
|
O=true
|
2021-03-14 12:58:09 +00:00
|
|
|
;;
|
2021-03-13 16:44:33 +00:00
|
|
|
|
2021-03-14 12:58:09 +00:00
|
|
|
"-t")
|
2021-03-13 16:44:33 +00:00
|
|
|
COL=TelNr
|
|
|
|
O=true
|
2021-03-14 12:58:09 +00:00
|
|
|
;;
|
2021-03-13 16:44:33 +00:00
|
|
|
|
2021-03-14 12:58:09 +00:00
|
|
|
"-c")
|
2021-03-13 16:44:33 +00:00
|
|
|
COL=Closing
|
|
|
|
O=true
|
2021-03-14 12:58:09 +00:00
|
|
|
;;
|
2021-03-13 16:44:33 +00:00
|
|
|
|
2021-03-14 12:58:09 +00:00
|
|
|
"-p")
|
2021-03-13 16:44:33 +00:00
|
|
|
COL=parent
|
|
|
|
O=true
|
2021-03-14 12:58:09 +00:00
|
|
|
;;
|
|
|
|
esac
|
2021-03-13 16:44:33 +00:00
|
|
|
|
2021-03-13 18:51:38 +00:00
|
|
|
if [ $O = true ]
|
2021-03-13 16:44:33 +00:00
|
|
|
then
|
2021-03-14 12:58:09 +00:00
|
|
|
LOC=$(head -1 $BASEPATH/Adressen.csv | tr ',' '\n' | nl |grep -w "$COL" | tr -d " " | awk -F " " '{print $1}') #calculating position of requested Col
|
2021-03-19 23:13:39 +00:00
|
|
|
grep "^$H," $BASEPATH/Adressen.csv | head -n 1 | cut -d "," -f$LOC | sed 's/"//g'
|
2021-03-13 16:44:33 +00:00
|
|
|
#Returning requested column, making sure to return only one result (even though redundant results shouldn't be possible)
|
2021-03-13 18:51:38 +00:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2021-03-13 19:45:17 +00:00
|
|
|
if [ $F = "-e" ]
|
|
|
|
then
|
2021-03-19 23:13:39 +00:00
|
|
|
NULL=$(grep "^$H," $BASEPATH/Adressen.csv)
|
2021-03-13 19:45:17 +00:00
|
|
|
ISH=$? #exit code. Der if-Block wird ausgeführt, wenn nichts gefunden wurde. (ISH steht für is handle)
|
|
|
|
if [ $ISH -eq 1 ]
|
|
|
|
then
|
|
|
|
read -p "Dieses Handle ist unbekannt. Neuen Datensatz anlegen? (j/n)" J
|
|
|
|
if [ $J = "j"]
|
|
|
|
then
|
|
|
|
eval $0 -g $H
|
|
|
|
exit 0
|
|
|
|
else
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
2021-03-14 13:24:52 +00:00
|
|
|
IFS=',' read -ra HEADER <<< $(grep "^handle," $BASEPATH/Adressen.csv) #read field keys
|
|
|
|
IFS=',' read -ra VALUES <<< $(grep "^$H," $BASEPATH/Adressen.csv) #read old values
|
2021-03-13 19:45:17 +00:00
|
|
|
# for loop startet bei 1, um die handle Spalte nicht zu editieren:
|
2021-03-14 13:24:52 +00:00
|
|
|
NEWLINE="$H"
|
2021-03-13 19:45:17 +00:00
|
|
|
for (( i=1; i<${#HEADER[@]}; i++ ));
|
|
|
|
do
|
|
|
|
read -rp "${HEADER[$i]} (${VALUES[$i]}): " NEWVAL
|
|
|
|
# echo "${HEADER[$i]} (${VALUES[$i]}): "
|
2021-04-09 09:45:27 +00:00
|
|
|
VALUES[$i]=$(echo ${NEWVAL:-${VALUES[$i]}} | sed -e s:,::g )
|
2021-03-14 13:24:52 +00:00
|
|
|
NEWLINE="$NEWLINE,${VALUES[$i]}" #already prepare output line
|
2021-03-13 19:45:17 +00:00
|
|
|
if [ ${HEADER[$i]} = "parent" ]
|
|
|
|
then
|
|
|
|
PARENT=${VALUES[$i]}
|
2021-03-14 12:58:09 +00:00
|
|
|
NULL=$(grep "^$PARENT" $BASEPATH/Adressen.csv) # findet alles, wenn $PARENT leer ist.
|
2021-03-13 19:45:17 +00:00
|
|
|
ISP=$? #exit code. Der if-Block wird ausgeführt, wenn nichts gefunden wurde.
|
|
|
|
if [ $ISP -eq 1 ]
|
|
|
|
then
|
|
|
|
eval $0 -g $PARENT
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
2021-03-14 13:24:52 +00:00
|
|
|
echo "$(grep -v "^$H," $BASEPATH/Adressen.csv)" > $BASEPATH/Adressen.csv
|
|
|
|
echo $NEWLINE >> $BASEPATH/Adressen.csv
|
2021-03-13 19:45:17 +00:00
|
|
|
fi
|
|
|
|
|
2021-03-13 18:51:38 +00:00
|
|
|
if [ $F = "-g" ]
|
|
|
|
then
|
2021-03-19 23:13:39 +00:00
|
|
|
NULL=$(grep "^$H," $BASEPATH/Adressen.csv)
|
2021-03-13 18:51:38 +00:00
|
|
|
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"}
|
2021-03-14 12:58:09 +00:00
|
|
|
echo "$H,$NAME,$ANREDE,$STREET\\\\$CITY,$FAX,$TEL,$CLOSING,$PARENT" >> $BASEPATH/Adressen.csv
|
|
|
|
NULL=$(grep "^$PARENT" $BASEPATH/Adressen.csv) # findet alles, wenn $PARENT leer ist.
|
2021-03-13 18:51:38 +00:00
|
|
|
ISP=$? #exit code. Der if-Block wird ausgeführt, wenn nichts gefunden wurde.
|
|
|
|
if [ $ISP -eq 1 ]
|
|
|
|
then
|
|
|
|
eval $0 -g $PARENT
|
|
|
|
fi
|
2021-03-13 16:44:33 +00:00
|
|
|
fi
|