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

5d6fdaf3b283ae0eb93a6414ec44744b17ac0bf5 — Johan Vandegriff 1 year, 5 months ago
initial files
11 files changed, 308 insertions(+), 0 deletions(-)

A add
A change
A display
A export
A lu
A print
A rem
A rolo
A test
A tmp.txt
A x
A  => add +33 -0
@@ 1,33 @@
#!/bin/sh

#
# Add someone into the phonebook
#

echo "Type in your new entry."
echo "When you are done, just type a single"
echo "RETURN on the line."
first=
entry=

while true
do
  echo -n ">> "
  read line

  if [ -n "$line" ]
  then
    entry="$entry$line^"

    if [ -z "$first" ]
    then
      first=$line
    fi
  else
    break
  fi
done
echo "$entry" >> $PHONEBOOK
sort -o $PHONEBOOK $PHONEBOOK
echo
echo "$first has been added to the phone book."

A  => change +52 -0
@@ 1,52 @@
#!/bin/sh

#
# change an entry in the phonebook
#

name=$1

#
#  get matching entries and save in temp file
#

grep "$name" $PHONEBOOK > /tmp/matches$$

if [ ! -s /tmp/matches$$ ]
then
  echo "I cannot  find $name in the phone book."
  exit 1
fi

#
# Display matching entries one at a time and confirm change.
#

exec < /tmp/matches$$

while read line
do
  . ~/programs/phonebook/display "$line"
  echo -n "Change this entry (y/n)? "
  read answer < /dev/tty

  if [ "$answer" = y ]
  then
    break
  fi
done
if [ "$answer" != y ]
then
  exit
fi
exec < /dev/tty
display $line > /tmp/ed$$
trap "" 2
cd /tmp
nano ed$$
grep -v "^$line$" $PHONEBOOK > /tmp/phonebook$$
tr '\n' '^' < /tmp/ed$$  >>/tmp/phonebook$$
echo  >>/tmp/phonebook$$
>>/tmp/phonebook$$
sort -o /tmp/phonebook$$ $PHONEBOOK
rm /tmp/ed$$ /tmp/phonebook$$

A  => display +21 -0
@@ 1,21 @@
#!/bin/sh

#
# Display entry from the phonebook
#

if [ -n "$@" ]
then
  echo "$@" | tr "^" "\n" > /home/johanv/programs/phonebook/tmp.txt
  #$0
fi

print=/home/johanv/programs/phonebook/print

$print ' --------------------------------\n'
$print '|   O                        O   |\n'
while read line
do
  $print '                                 |\r| '$line'\n'
done < /home/johanv/programs/phonebook/tmp.txt
$print ' --------------------------------\n'

A  => export +15 -0
@@ 1,15 @@
#!/bin/sh

#
# Put a adress in a file
#

name=$1
file=$2

if . ~/programs/phonebook/lu $name
then
  grep $name $PHONEBOOK > $file
else
  exit 1
fi

A  => lu +26 -0
@@ 1,26 @@
#!/bin/sh

#
# Look someone up on the phonebook
#

name=$1

grep $name $PHONEBOOK >/tmp/matches$$

    if [  ! -s /tmp/matches$$ ]
    then
        if [ "$name" = "^" ]
        then
          echo "Your phone book is empty."
        else
          echo "I cannot find $name in the phone book."
        fi
    else
      while read line
      do
        . ~/programs/phonebook/display "$line"
      done </tmp/matches$$
    fi

rm /tmp/matches$$

A  => print +3 -0
@@ 1,3 @@
#!/bin/sh

echo -n "$@" | tr -t " $@$@" " $@"

A  => rem +38 -0
@@ 1,38 @@
#!/bin/sh

#
# remove someone from thee phonebook
#

name=$1
grep "$name" $PHONEBOOK > /tmp/matches$$

if [ -s /tmp/matches$$ ]
then
  echo "I cannot find $name in the phone book."
  exit 1
fi
exec < /tmp/matches$$

while read line
do
  . ~/display "$line"
  echo -n "Remove this entry "'(y/n)?'
  read answer < /dev/tty

  if [ "$answer = y" ]
  then
    break
  fi
done

rm /tmp/matches$$

if [ "$answer = y" ]
then
  if grep -v "^$line$" $PHONEBOOK > /tmp/phonebook$$
  then
    mv /tmp/phonebook$$ $PHONEBOOK
    echo "Entry not removed"
  fi
fi

A  => rolo +100 -0
@@ 1,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

A  => test +12 -0
@@ 1,12 @@
#!/bin/sh
cd #~/programs/other
ls -a | while read line
do
  if [ -f "$line" ]
  then
    echo "<=========>"
    echo "$line:"
    head -1 "$line"
  fi
done
read a

A  => tmp.txt +5 -0
@@ 1,5 @@
hi
how
are
you?
1111111111111111111112222222222222222

A  => x +3 -0
@@ 1,3 @@
#!/bin/sh
/home/johanv/programs/phonebook/display "hi"
read a