LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-24-2009, 01:04 PM   #1
sms552
LQ Newbie
 
Registered: Nov 2009
Posts: 2

Rep: Reputation: 0
bash script help...


i have a basic script here to that adds info to a source file... i have the script working but if a user makes a mistake when typing and doesnt notice it the script will not let them go back to a previous line to correct the mistake... just starting to get into bash so any help would be greatly appriciated..


#!/bin/bash
trap "rm ~/tmp/* 2> /dev/null; exit" 0 1 2 3
phonefile=./corp_phones
looptest=y
while [ $looptest = y ]
do
clear
tput cup 1 4 ; echo "Corporate Phone List Additions"
tput cup 2 4 ; echo "=============================="
tput cup 4 4 ; echo "Phone Number: "
tput cup 5 4 ; echo "Last Name : "
tput cup 6 4 ; echo "First Name : "
tput cup 7 4 ; echo "Middle Init : "
tput cup 8 4 ; echo "Dept # : "
tput cup 9 4 ; echo "Job Title : "
tput cup 10 4; echo "Date Hired : "
tput cup 12 4; echo "Add another? (y)es or (q)uit "
tput cup 4 18; read phonenum
if [ "$phonenum" = 'q' ]
then
clear ; exit
fi
tput cup 5 18 ; read lname
tput cup 4 18 ; read phonenum
tput cup 6 18 ; read fname
tput cup 7 18 ; read midinit
tput cup 8 18 ; read deptno
tput cup 9 18 ; read jobtitle
tput cup 10 18; read datehired


if [ "$lname" > " "]
then
echo "$phonenum:$lname:$fname:$midinit:$deptno:$jobtitl e:$datehired" >> $phonefile
fi
tput cup 12 33 ; read looptest
if [ "$looptest" = 'q' ]
then
clear ; exit
fi
 
Old 11-24-2009, 02:26 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Independance, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 160Reputation: 160
It helps readability a bit if you enclose things in a code tag, but aside from that, you could test each time for the next input to be p or something similiar and if it is then renter that line. Alternately you could put each in a loop and do a "Is whatever correct (y) or (n)?" and not proceed till it was y.
 
Old 11-24-2009, 02:27 PM   #3
roybal
LQ Newbie
 
Registered: Sep 2009
Posts: 8
Blog Entries: 8

Rep: Reputation: 0
General strategy to edit last line

you could use tail -n 1 $phonefile to get the last line of the file, sed to delete the line from the file, awk to split it into variables, and then edit it and add it back on to the end of the file.

/bithead
 
Old 11-24-2009, 02:39 PM   #4
sms552
LQ Newbie
 
Registered: Nov 2009
Posts: 2

Original Poster
Rep: Reputation: 0
rewrote code... same result...

i redid the code... added some things and seperated things by loop thinking if i ran seperate loops it would work.. it doesnt.. same issue.. when i enter a - "dash" into an empty line i would like it to go to the previous line.. which it does... only once... if i try it again it will just put a dash on the line..


trap "rm ~/tmp/* 2> /dev/null; exit" 0 1 2 3
phonefile=/home/scott/Desktop/chapter7/source/corp_phones
looptest=y
while test "$looptest" = "y"
do
clear
tput cup 1 4; echo "Corporate Phone List Additions"
tput cup 2 4; echo "=============================="
tput cup 4 4; echo "Phone Number: "
tput cup 5 4; echo "Last Name : "
tput cup 6 4; echo "First Name : "
tput cup 7 4; echo "Middle Nam : "
tput cup 8 4; echo "Dept # : "
tput cup 9 4; echo "Job Title : "
tput cup 10 4; echo "Date Hired : "
tput cup 12 4; echo "Add Another (y)es or (q)uit: "
tput cup 4 18; read phonenum
if test $phonenum = "q"
then
clear; exit
fi
# Check to see if the phone number already exists
while grep "$phonenum" $phonefile > ~/tmp/temp/temp
do
tput cup 19 1 ; echo "This number has already been assigned to: "
tput cup 20 1 ; tr ':' ' ' < ~/tmp/temp/temp
tput cup 21 1 ; echo "Press ENTER to continue... "
read prompt
tput cup 4 18 ; echo " "
tput cup 4 18 ; read phonenum
if test "$phonenum" = "q"
then
clear ; exit
fi
done
tput cup 5 18; read lname
while test "#lname" = "-"
do
tput cup 4 18 ; echo " "
tput cup 4 18 ; read phonenum
if test "$phonenum" = "q"
then
clear ; exit
fi
tput cup 5 18 ; read lname
exit;
done
tput cup 6 18; read fname
while test "$fname" = "-"
do
tput cup 5 18 ; echo " "
tput cup 5 18 ; read lname
if test "$lname" = "q"
then
clear ; exit
fi
tput cup 6 18 ; read fname
exit;
done
tput cup 7 18; read midinit
while test "$midinit" = "-"
do
tput cup 6 18 ; echo " "
tput cup 6 18 ; read fname
if test "$fname" = "q"
then
clear ; exit
fi
tput cup 7 18 ; read midinit
done
tput cup 8 18; read deptno
while test "$deptno" = "-"
do
tput cup 7 18 ; echo " "
tput cup 7 18 ; read midinit
if test "$midinit" = "q"
then
clear ; exit
fi
tput cup 8 18 ; read deptno
done
tput cup 9 18; read jobtitle
while test "$jobtitle" = "-"
do
tput cup 8 18 ; echo " "
tput cup 8 18 ; read deptno
if test "$deptno" = "q"
then
clear ; exit
fi
tput cup 9 18 ; read jobtitle
done
tput cup 10 18; read datehired
while test "$datehired" = "-"
do
tput cup 9 18 ; echo " "
tput cup 9 18 ; read jobtitle
if test "$jobtitle" = "q"
then
clear ; exit
fi
tput cup 10 18 ; read datehired
done
#Check to see if last name is not blank before you write to disk.
if test "$lname" != ""
then
echo "$phonenumb:$lname:$fname:$midinit:$deptno:$jobtitle:$datehired" >> $phonefile
fi
tput cup 12 33 ; read looptest
if test "$looptest" = "q"
then
clear; exit
fi
done

Last edited by sms552; 11-24-2009 at 02:41 PM.
 
Old 11-24-2009, 03:07 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: back to Arch
Posts: 16,641

Rep: Reputation: 423Reputation: 423Reputation: 423Reputation: 423Reputation: 423
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 11-24-2009, 10:14 PM   #6
gratuitous_arp
LQ Newbie
 
Registered: Jul 2009
Posts: 28

Rep: Reputation: 17
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
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 06:00 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 07:01 PM
[bash] having trouble debugging this bash script. jons Programming 4 02-08-2007 06:51 AM


All times are GMT -5. The time now is 01:14 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration