~johanvandegriff/unix-shell-programming-revised-edition-programs

ref: 2b467c818c691e6c743c2e147522f97443d4ba3c unix-shell-programming-revised-edition-programs/rolo -rw-r--r-- 2.4 KiB
2b467c81Johan Vandegriff added license 1 year, 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/sh

#
# rolodex program to look up add remove and change entries from the phonebook
#

# echo -n "Please enter a phonebook to be used: "
# read PHONEBOOK
#: ${PHONEBOOK:=$HOME/programs/phonebook/phonebook.txt}
PHONEBOOK=/home/johanv/programs/phonebook/phonebook.txt
export PHONEBOOK

if [ ! -f "$PHONEBOOK" ]
then
  echo "$PHONEBOOK does not exist or is not an ordinary file!"
  echo -n "should I create it for you(y/n)?"
  read answer
  if [ "$answer" != "y" ]
  then
    exit 1
  fi
  > $PHONEBOOK || exit 1
fi

if [ -f "$HOME/.phonebook" ]
then
  PHONEBOOK="$HOME/.phonebook"
fi

if [ "$#" -ne 0 ]
then
  if [ "$1" = "-u" ]
  then
    PHONEBOOK="$HOME/$2"
  else
    . ~/phonebook/lu "$@"
    exit
  fi
fi

trap "continue" 2

while true
do
  echo '
  --------------------------------------------
  | Would you like to:                       |
  | 1.Look Someone up                        |
  | 2.Add someone to the phonebook           |
  | 3.Remove someone from the phone book     |
  | 4.Change an entry in the phone book      |
  | 5.Put one or more entries in a file      |
  | 6.List all the entries in the phone book |
  | 7.Exit this program                      |
  --------------------------------------------'
  echo -n 'Please select one of the above (1-7): '
  read choice
  echo
  case "$choice"
  in
   1) echo -n "Enter name to look up "
      read name
      if [ -z "$name" ]
      then
        echo "Lookup ignored"
      else
        . ~/programs/phonebook/lu $name || echo "$name is not in your phonebook."
      fi;;
   2) ~/programs/phonebook/add;;
   3) echo -n "Enter name to be removed "
      read name
      if [ -z "$name" ]
      then
        echo "Removal ignored"
      else
        . ~/programs/phonebook/rem $name || echo "$name is not in your phonebook."
      fi;;
   4) echo -n "Enter name to change "
      read name
      if [ -z "$name" ]
      then
        echo "Change ignored"
      else
        . ~/programs/phonebook/change $name || echo "$name is not in your phonebook."
      fi;;
   5) echo -n "Enter name to export "
      read name
      if [ -z "$name" ]
      then
        echo "Export ignored"
      else
        echo -n "Enter file to export to "
      read file
        . ~/programs/phonebook/export $name $file || echo "$name is not in your phonebook."
      fi;;
   6) cat ~/programs/phonebook/phonebook.txt | tr '\n' '\n\n' | tr '^' '\n';;
   7) exit 0;;
   *) echo "Bad choice";;
  esac
done