Fun little problem sms552! While BASH is probably not an optimal interface for data entry, it can certainly rise to the occasion...
This program is similar to what you are wanting to do. It prompts for the numbers of three colors: red, green, and blue, and allows you to go back to multiple past prompts using the dash (-), to change information before you commit it. When you enter a dash, it takes you to the previous line, and remembers the furthest line down that you have completed. This means you could be filling in the last item in a list and be able to go back to change the first item in a list, without having to re-enter all of the items in between.
I didn't look through all of your code to see where you might have had problems... but I did see that one of your variable names were off ("#lname" instead of "$lname"). Without indentation, it is much more difficult for others (and yourself) to understand what it is your code is trying to accomplish... neat indentation makes things much easier for everyone.
You can add your own checks for redundant data (for phone numbers, in your case). The comments should be able to guide you along with most of it, but feel free to ask questions. There is a lot of other stuff you could do to make this easier on data entry people, but this general idea should be able to help you along to the program you will be happy with.
Code:
#!/bin/bash
MAX_ROWS=4 # How many rows will the user fill out (plus one)?
DATA_FILE="/home/user/shell/temp.txt"
r=1 # What "r"ow am I on?
b=0 # Do I want to go "b"ack any rows?
loop=1 # Used later to check for valid input
touch $DATA_FILE 2>/dev/null # Make sure the file exists
if [ $? -ne 0 ] ; then
echo "Data file ($DATA_FILE) does not exist, exiting."
exit 1
fi
clear
tput cup 1 4 ; echo "Red number: "
tput cup 2 4 ; echo "Green number: "
tput cup 3 4 ; echo "Blue number: "
while : ; do # loop until you hit an "exit" statement
tput cup `expr $r - $b` 19
read input
if [ "$input" == "-" ] ; then
if [ `expr $r - $b` -eq 1 ] ; then
continue # If we're already on row 1, we can't go back any more
fi
tput cup `expr $r - $b` 19
echo " "
if [ $b -ge 1 ] ; then # if we already went back at least one row
# re-write the row so it looks nice
tput cup `expr $r - $b` 19
case `expr $r - $b` in
1) echo $red ;; # If we were on row 1, re-print that value
2) echo $green ;; # (We set these a little later on)
3) echo $blue ;;
esac
fi
let b++ # Increase b by 1 every time you input -
continue # Then go to the next iteration of the while loop
elif [ "$input" == "q" ] ; then
exit 0
fi
case `expr $r - $b` in
1) red=$input ;; # If we're on row 1, this is red value
2) green=$input ;; # If we're on row 2, this is the green value
3) blue=$input ;; # and so on...
esac
if [ $b -eq 0 ] ; then
let r++ # Increment the row only if b is 0, so we don't skip rows
fi
b=0 # Reset b
if [ $r -eq $MAX_ROWS ] ; then # If we filled out all our rows...
echo "$red:$green:$blue" >> "$DATA_FILE" # Write it out
while [ $loop -eq 1 ] ; do # Until we get valid input, keep asking...
echo -n "Add another record? (y)es or (n)o: "
read answer
case $answer in
y) clear
tput cup 1 4 ; echo "Red number: " # Set up a new screen
tput cup 2 4 ; echo "Green number: "
tput cup 3 4 ; echo "Blue number: "
r=1
loop=0 ;;
n) clear ;
exit 0 ;;
*) echo "Please answer \"y\" for yes or \"n\" for no." ;;
esac
done
fi
loop=1 # Remember to reset your loop variable
done