#!/bin/ksh # Pook - a mini phone book # Version 1.1 - Aug 2007 # Author: frad - http://frad.effraie.org # Notes : # A directory $HOME/bin is necessary to use pook # $HOME/bin/datapook file is limited to 999 records usage() { print "Usage: pook [-a] [-d] [-g] [-h] [-m] [-p] [-s string]\n" print " : Print complete list" print " -a : Add a new contact" print " -d : Delete a contact" print " -g : Print list with gsm only" print " -h : Print help" print " -m : Print list with mail only" print " -p : Print list with phone only" print " -s string: Search contact" } # Function rdwt : exit if $1 not rw rdwt() { if [ ! -r $1 ] ; then print "Error: $1 not readable." exit 1 fi if [ ! -w $1 ] ; then print "Error: $1 not writeable." exit 1 fi } # Verify if $HOME/bin exists HBIN=$HOME/bin if [ ! -d ${HBIN} ] ; then print "Error: no directory ${HBIN}" exit 1 fi rdwt ${HBIN} # Verify if $HOME/bin/datapook exists, create else DTPK=$HOME/bin/datapook if [ ! -f ${DTPK} ] ; then print -n "Creating ${DTPK} ... " touch ${DTPK} rdwt ${DTPK} sleep 0.5 ; print "Ok" exit 0 fi rdwt ${DTPK} # Limit size of $HOME/bin/datapook to 999 lines if [ -s ${DTPK} ] && [ $(sed -n '$=' ${DTPK}) -gt 999 ] ; then TMPF=$(mktemp ${HBIN}/pkfile.X) print "Warning: ${DTPK} limited to 999 lines" head -999 ${DTPK} > ${TMPF} mv ${TMPF} ${DTPK} fi # Function affich : print a list from $HOME/bin/datapook affich() { awk -F: 'BEGIN { print "ID FIRSTNAME LASTNAME PHONE GSM MAIL" } { printf "%-3s %-12s %-12s %-11s %-11s %s\n", $1, $2, $3, $4, $5, $6 }' ${DTPK} } # Function renum : in alphabetic and renum $HOME/bin/datapook renum() { TMPFA=$(mktemp ${HBIN}/pkfile.X) TMPFB=$(mktemp ${HBIN}/pkfile.XX) sort -t : +2 ${DTPK} > ${TMPFA} cut -d: -f2,3,4,5,6 ${TMPFA} > ${TMPFB} sed = ${TMPFB} | sed 'N;s/\n/\:/' > ${DTPK} rm -f ${TMPFA} ${TMPFB} } # Print complete list: no option if [ "$1" == "" ] ; then renum affich exit 0 fi while [ "$1" != "" ] do case "$1" in -a) FLGA="true" ;; -d) FLGD="true" ;; -g) FLGG="true" ;; -m) FLGM="true" ;; -p) FLGP="true" ;; -h) FLGH="true" ;; -s) shift ; SHCT="$1" ;; *) usage ; exit 1 ;; esac shift done # Print list with phone: option -p if [ "${FLGP}" ] ; then renum awk -F: 'BEGIN { print "ID FIRSTNAME LASTNAME PHONE" } { printf "%-2s %-12s %-12s %s\n", $1, $2, $3, $4 }' ${DTPK} exit 0 fi # Print list with gsm: option -g if [ "${FLGG}" ] ; then renum awk -F: 'BEGIN { print "ID FIRSTNAME LASTNAME GSM" } { printf "%-2s %-12s %-12s %s\n", $1, $2, $3, $5 }' ${DTPK} exit 0 fi # Print list with mail: option -m if [ "${FLGM}" ] ; then renum awk -F: 'BEGIN { print "ID FIRSTNAME LASTNAME MAIL" } { printf "%-2s %-12s %-12s %s\n", $1, $2, $3, $6 }' ${DTPK} exit 0 fi # Exit if options -da or -ad used if [ "${FLGA}" ] && [ "${FLGD}" ] ; then usage exit 1 fi # Print a new line in $/HOME/bin/datapook: option -a if [ "${FLGA}" ] ; then if [ -s ${DTPK} ] && [ $(sed -n '$=' ${DTPK}) -gt 998 ] ; then print "Error: ${DTPK} limited to 999 records" exit 1 fi print "Fill the form to add a new contact in your phone book:" print -n "Firstname: " ; read FSTN FSTN=$(echo ${FSTN} | sed "s/\(.\{11\}\)\(.*\)/\1/") print -n "Lastname: " ; read LSTN LSTN=$(echo ${LSTN} | sed "s/\(.\{11\}\)\(.*\)/\1/") print -n "Phone: " ; read PHON PHON=$(echo ${PHON} | sed "s/\(.\{10\}\)\(.*\)/\1/") print -n "GSM: " ; read GSM GSM=$(echo ${GSM} | sed "s/\(.\{10\}\)\(.*\)/\1/") print -n "Mail: " ; read MEL MEL=$(echo ${MEL} | sed "s/\(.\{20\}\)\(.*\)/\1/") NEWL="N:"${FSTN}":"${LSTN}":"${PHON}":"${GSM}":"${MEL} echo "${NEWL}" >> ${DTPK} exit 0 fi # Delete a line from $HOME/bin/datapook: option -d if [ "${FLGD}" ] ; then NBLI=$(sed -n '$=' ${DTPK}) if [ "${NBLI}" -lt 1 ] ; then print "Error: file empty" exit 1 fi renum affich print -n "Enter a number ID or [C] to Cancel: " ; read IDCL case ${IDCL} in C|c) exit 0 ;; [1-9]|[1-9][0-9]|[1-9][0-9][0-9]) if [ "${IDCL}" -gt "${NBLI}" ] ; then print "Error: incorrect value." exit 1 else TMPFC=$(mktemp ${HBIN}/pkfile.X) sed "${IDCL}d" ${DTPK} > ${TMPFC} mv ${TMPFC} ${DTPK} print "Contact ${IDCL} removed." exit 0 fi ;; *) print "Error: incorrect value" ; exit 1 ;; esac fi # Print line(s) from $HOME/bin/datapook with search : option -s if [ "${SHCT}" != "" ] ; then TLNE=$(grep -i -c ${SHCT} ${DTPK}) case ${TLNE} in 0) print "No contact found with \"${SHCT}\"" ;; *) grep -i ${SHCT} ${DTPK} | awk -F: 'BEGIN { print "ID FIRSTNAME LASTNAME PHONE GSM MAIL"} { printf "%-2s %-12s %-12s %-11s %-11s %s\n", $1, $2, $3, $4, $5, $6 }' ;; esac exit 0 fi # Exit if more than 1 argument used if [ $# -gt 1 ] ; then print "Error: too many arguments." exit 1 fi