uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

teldb (3467B)


      1 #!/bin/sh
      2 
      3 # ΧΡΗΣΤΟΣ ΜΑΡΓΙΩΛΗΣ - [REDACTED]
      4 
      5 # We'll use this later.
      6 emptylnregex="^\ *$"
      7 
      8 main() {
      9         # Parse command line options, exit if no option was passed.
     10         case ${1} in
     11                 -a) newentry ;;
     12                 -l) list ;;
     13                 -s) sortcol ${2} ;;
     14                 -c) filter ${2} ;;
     15                 -d) deleteln ${2} ${3} ;;
     16                 -n) emptyln ;;
     17                 *) usage ;;
     18         esac
     19 }
     20 
     21 usage() {
     22         # `{0##*/}` means the first argument (i.e the script's name) with
     23         # its path stripped, if it exists. 
     24         # The {} around the options denote that only one option has to be
     25         # passed to the script.
     26         echo "usage: ${0##*/} {-a | -l | -n | -c keyword | -d keyword {-b | -r} | -s col}" 1>&2
     27         echo "" 1>&2
     28         echo "options:" 1>&2
     29         echo "-a        add a new entry" 1>&2
     30         echo "-l        list contents of file" 1>&2
     31         echo "-n        count empty lines and ask if they should be deleted" 1>&2
     32         echo "-c        filter keyword" 1>&2
     33         echo "-d        delete lines containing a keyword followed by one of the options below" 1>&2
     34         echo "          -b replaces line with an empty line" 1>&2
     35         echo "          -r removes line completely" 1>&2
     36         echo "-s        sort based on specified column (e.g ${0##*/} -s 3)" 1>&2
     37 
     38         exit 1
     39 }
     40 
     41 # Exit with a `usage` message if the string that was passed is empty.
     42 # This is used to make sure that we'll not write empty fields in `catalog`.
     43 errempty() {
     44         test ! -z "${1}" || usage
     45 }
     46 
     47 # Skip empty lines
     48 skipempty() {
     49         grep -v "${emptylnregex}"
     50 }
     51 
     52 # -a option - Make a new entry for `catalog`.
     53 newentry() {
     54         # Read fields and include an error check so that
     55         # we don't pass an empty field.
     56         read -erp "First name: " fname
     57         errempty "${fname}"
     58 
     59         read -erp "Last name: " lname
     60         errempty "${lname}"
     61 
     62         read -erp "Town: " town
     63         errempty "${town}"
     64 
     65         read -erp "Phone number: " phone
     66         errempty "${phone}"
     67 
     68         # Append all the things we've read to `catalog`.
     69         printf "%s %s %s %s\n" "${fname}" "${lname}" "${town}" "${phone}" >> catalog
     70 }
     71 
     72 # -l option - Print `catalog` with each line numbered. It also skips empty lines.
     73 list() {
     74         nl catalog | skipempty
     75 }
     76 
     77 # -s option - Sort `catalog` by column. We pass the desired column in `main`.
     78 sortcol() {
     79         errempty "${1}"
     80         sort -k "${1}" catalog | skipempty
     81 }
     82 
     83 # -c option - Print only the lines that match a specific keyword.
     84 filter() {
     85         errempty "${1}"
     86         grep "${1}" catalog
     87 }
     88 
     89 # -d option - Delete a line.
     90 deleteln() {
     91         errempty "${1}"
     92         errempty "${2}"
     93 
     94         # Parse sub-options.
     95         case ${2} in
     96                 # Replace line with an empty line.
     97                 -b) sed -i "s/.*${1}.*//" catalog ;;
     98                 # Just delete the line.
     99                 -r) sed -i "/${1}/d" catalog ;;
    100         esac
    101 }
    102 
    103 # -n option - Count empty lines and delete them if the user answers with `y`.
    104 emptyln() {
    105         printf "Empty lines in 'catalog': "
    106         grep "${emptylnregex}" catalog | wc -l
    107         read -erp "Delete them (y/n)? " del
    108         # Delete lines using `deleteln` with `-r` option so that it
    109         # just deletes the line, without replacing it with an empty one.
    110         test "${del}" = "y" && deleteln "${emptylnregex}" "-r"
    111 }
    112 
    113 # Pass all command line arguments to `main`.
    114 main "$@"